0
function imageHandler(myToDo,myimageId)
{ 
    $.get('{{path('imageHandler')}}',
        {imageId: myimageId, toDo: myToDo},  
        function(response) {
            if(response.code == 100 && response.success) {
                //success
                alert(myToDo+' was sent');
            }
            else if(response.code == 200) {
                //code 200 = user not logged in
            }; 
        }, "json");
}

This is the working ajax code for a symfony2 application im writing.

I'm using $.get from jquery to send the data, how is it possible to write this function without jQuery?, I have already looked into other similar questions but I don't understand the answers.

Already tried this

function imageHandler(myToDo,myimageId)
{ 
    var hr = new XMLHttpRequest(); 
    var vars = {imageId: myimageId, toDo: myToDo};

    hr.open("GET", "{{path('imageHandler')}}", true); 
    hr.setRequestHeader("Content-type", "application/json"); 
    hr.onreadystatechange = function() 
    {
      if(hr.code == 100 && hr.success)
      {
        //success
      }
       else if(hr.code == 200) 
       {
        //code 200 = user not logged in
       }
    } 
    hr.send(vars);  
}

here is the answer:

A co-worker just answered the question. Thanks for your help, here is the answer

function imageHandler(myToDo,myimageId)
{ 
    var xml = new XMLHttpRequest();  
    xml.open("GET", "{{path('imageHandler')}}", true); 
    xml.setRequestHeader("Content-type", "application/json"); 

    xml.onreadystatechange = function() 
    {
       var serverResponse = JSON.parse(xml.responseText);

       if(serverResponse.code == 100 && serverResponse.success)
       {
        //success 
       }
       else if(serverResponse.code == 200) 
       {
          //not logged in

       }

        xml.send({imageId: myimageId, toDo: myToDo});   
   }
}
Gabriel ThaKid
  • 857
  • 3
  • 9
  • 19

2 Answers2

1

Get your HTTP verb right

hr.open("GET", "{{path('imageHandler')}}", true); 

You are making a GET request but trying to send a request with a body. You need to use POST (or maybe PUT), not GET.

hr.open("POST", "{{path('imageHandler')}}", true); 

Alternatively: The jQuery code you wrote is making a GET request and encoding the data as a query string. You could do that instead.


Send the data format you claim you are sending

hr.setRequestHeader("Content-type", "application/json"); 

You say you are sending JSON but…

var vars = {imageId: myimageId, toDo: myToDo};
hr.send(vars);  

vars is a JavaScript object. When converted to a string, it will look something like `"[Object object]". If you want to send JSON, you need to make JSON.

var vars = {imageId: myimageId, toDo: myToDo};
hr.send(JSON.stringify(vars));  

Note that the server side program that processes the data will need to be set up to handle JSON requests.

Alternatively: The jQuery code you wrote is sending the data encoded as a query string and not as JSON. You could do that instead.


You need to check the response status correctly

  if(hr.code == 100 && hr.success)

Where does the code property come from? Where does the success property come from? Neither of them appear in the documentation you said you read.

If you are processing the data using onreadystatechange then you need to test if:

  • The ready state is DONE
  • The HTTP Status is OK

Such:

if (hr.readyState == 4 && hr.status == 200)
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks for answering, the code and success variables are returned from a symfony2 controller like this $response = array("code"=>200,"success"=>false); return new Response(json_encode($response)); – Gabriel ThaKid Jan 30 '14 at 11:30
  • Then, after you've established the status of the response is OK, you need to parse the text content of the response to a JavaScript object and then run your original `if` statement on *that*, not the XHR object. – Quentin Jan 30 '14 at 11:31
0

in javascript, here's something you can try:

req = new XMLHttpRequest();
req.onreadystatechange=function() { 
    status = req.status; //this is your response.code
 responseHtml = req.responseText; }

req.open("GET","your ajax file here",true);
req.send();

This is just an idea about ajax request in javascript. more info here.