-1

Im new to Ajax and im at the point that i get data.The only thing is i cant return my data i get undefine etc. Here is the code:

function select_aragement(arragament){
    var arrst = arragament;
    var arrsplit = arrst.split("|");
    var periode = arrsplit[0];
    var id = arrsplit[1];

    var data =$.ajax({
        type: "POST",
        url: 'ajax/prijzen.php',
        data:{
            id: id,
            periode:periode
        },
        dataType: 'json'
    });
    return data.responseText;
}

In this code, arragement is an array.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Bart Klaster
  • 53
  • 3
  • 11

3 Answers3

1

data.responseText isn't available when the AJAX call is still being done. You will need to wait after the AJAX closure and use the .done() function to make sure the call has completed.

This will look somewhat like this:

$.ajax({ 
  type: 'POST',
  url: 'ajax/prijzen.php',
  data: { what: 'ever' },
  dataType: 'json'
}).done(function (data) {
  console.log(data);
});

Greetings back from the Netherlands!

Bjorn 'Bjeaurn' S
  • 3,861
  • 2
  • 18
  • 29
1

The response data from ajax will not be available until the ajax request is complete. So,try using like success as follows :

function select_aragement(arragament){
 var arrst = arragament;
 var arrsplit = arrst.split("|");
 var periode = arrsplit[0];
 var id = arrsplit[1];

     var data =$.ajax({
       type: "POST",
       url: 'ajax/prijzen.php',
       data:{
           id: id,
          periode:periode
       },
       dataType: 'json'
       success:function(data){
         return data.responseText;
       }
    });    
}
Prabhuram
  • 1,268
  • 9
  • 15
1

Bjorn is just right. Always consider waiting for your asynchronous calls being finished ;)

There are three types of callbacks: done (success is deprecated), fail (you should always do something on fail) and always, which is 'always' called (whether the request fails or not)

I'd suggest you to read this: http://api.jquery.com/jQuery.ajax/