Jamie McAllister
Jamie McAllister

Reputation: 729

jQuery AJAX request not receiving response

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>

Upvotes: 1

Views: 1217

Answers (2)

Sandeep
Sandeep

Reputation: 431

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'

Upvotes: 2

Dusty
Dusty

Reputation: 384

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

Upvotes: 0

Related Questions