Jquery AJAX request not receiving response

359
November 21, 2016, at 8:07 PM

I'm having some bother with part of an application I'm making.

I need to send an AJAX request to get some information from an XML file. The server should be returning the information in a son format. I have been using the same code throughout both server, and client-side code. All the other functions work. This function does not even receive a response

On the PHP server i have this code: Main.php:

if($_GET["fn"] == "rLoad")
{
    echo $handlerP->toJSON($handlerP->getPRoles($_GET["projectID"]));
}

ProjectHandler.php:

function getPRoles($projectID){
    $sort = new ModuleSorter();
    $xml = self::getxml();
    $result = array();
    foreach($xml->children() as $project){
        if($project->id == $projectID){
            foreach($project->roles->children() as $role){
                array_push($result, array('id' => $role->id, 'name' => $role->name));
            }
        }
    }
    $res = $sort->alphabetically($result);
    return $res;
}
function toJSON($data){
    $result  = json_encode($data);
    return $result;
}
function getXml(){
    $xml=simplexml_load_file("../data/projects.xml") or die("ERROR: Unable to read file");
return $xml;
}

From the Client side I use the following code:

function loadRoles(project){
    console.log("Starting role retrieval")
    var data = {
      "fn" : "rload",
      "projectID": project,
      "ajax" : "true"
    };
    $.ajax({
      type: "GET",
      url: SERVICE_URL,
      data: data,
      contentType: "application/json",
      success: function(response) {
        var i, list = "";
        console.log(response);
            for (i = 0; i < response.length; i++) {
                list += formatOption(response[i].id[0], response[i].name[0]);
                $("#theRole").html(list).selectmenu('refresh');
            }
            },
        done: function(response){
            console.log(response);
        }   
    });
    console.log("end of role retrieval");        
}

I don't get any kind of response from the console.logs, and when I look under the network tab from google chrome's developer tools I see the message this request has no response data available

A sample of the projects XML file:

<projects>
    <project>
        <id>adultScotland</id>
        <name>Adults(Scotland)</name>
        <roles>
            <role>
                <id>projectM</id>
                <name>Project Manager</name>
            </role>
        </roles>
    </project>
</projects>

If anybody can see the cause of this problem I would be grateful for the help :)

Thanks

Answer 1

You are sending this

var data = {
  "fn" : "rload",
  "projectID": project,
  "ajax" : "true"
};

You are sending fn=rload from client side and on the server

if($_GET["fn"] == "rLoad")
{
    echo $handlerP->toJSON($handlerP->getPRoles($_GET["projectID"]));
}

you are comparing it with rLoad. This will never be true. 'rload'!='rLoad'

Answer 2

On client side try to use dataType : 'xml', and check if the domain of the request is different than the origin.

Rent Charter Buses Company
READ ALSO
HTML modified jQuery listener

HTML modified jQuery listener

I have created a chatbox in jQuery / MySQL and PHP. [nnn]To display new messages, I refresh the #chatcontent on my page using a POST HTTP request and the $('#chatcontent').

424
jQuery disable, and enable bxslider on window resize

jQuery disable, and enable bxslider on window resize

I've got some problem with bxslider. [nnn]I want to disable it when user resize the window under 1051px and enable again when user resize over 1051px.

569
Bootstrap Modal not working in Chrome or FireFox

Bootstrap Modal not working in Chrome or FireFox

I have a Modal Popup window that brings up images on my website. Everything works great in Microsoft Edge Browser, however, when I go to FireFox or Chrome, the Modal pops up, but doesn't show up solid and you can see through it to the background.

789
Parallax only works on page refresh, not on redirect

Parallax only works on page refresh, not on redirect

I am using the Materialize css parallax. It works fine on the first page load up, but when it is redirected to any other page in the site, none of the parallaxes render the pictures.

327