0

I have a custom function. Inside this function is an $.ajax call that returns a string that I want to convert to JSON with JSON.parse and then return.

Now, I figured out how to return the string and then JSON.parse on other side when the data is returned. However, when I try to convert the string to JSON already in the function and return it – this does not work and I do not understand why.

My guess is that I do not understand how to handle the $.ajax return-object and how to retrieve the responseText from this object.

Here is my code;

$.plugin.ownfunction = function(options) {

    var promised_data = $.ajax({
            method: "POST",
            url: "plugin/php/somephp.php",
            data: opts
    })


    // this does not work
    // promised_data = JSON.parse(promised_data);

    // this also does not work
    // promised_data = JSON.parse(promised_data.responseText);

    return promised_data;
};

on the other side I do this;

$.database.readbehavior({ some options }).done(function(data) {

        // this does work – but I would like to do the JSON.parse already in the function
        var x = JSON.parse(data);
        console.log(x);


});

edit: I am sorry to disagree – but this is not a duplicate of the How to return the response from an asynchronous call? topic. I managed to return a response from an asynchronous call. The Problem is the format of this response that I have trouble with.


solution: As Hacketo pointed out in the comments; Adding dataType : "json" to the $.ajax call returns the data as JSON. Problem solved! Thank You!

grueb
  • 123
  • 11
  • 1
    What are all these `return`? anything that is after a `return` statement will not be executed. In this case `def.resolve();` will not be executed after doing `return data;` . jQuery can handle JSON response [see docs](http://api.jquery.com/jquery.ajax/) with adding `dataType : "json"` to the `$.ajax` settings – Hacketo Aug 06 '15 at 11:48
  • in your ajax call use this: `success: function(msg){promised_data = $.parseJSON(msg)}` Also remove `var promised_data = ` before ajax call and just declare your variable. – classicalConditioning Aug 06 '15 at 11:53
  • I see. I cleaned up the code – now there is only one return. – grueb Aug 06 '15 at 11:53
  • @grueb I updated my comment with jQuery stuff – Hacketo Aug 06 '15 at 11:54
  • @Tushar please read the entire posts before answering or flagging them.. – Hacketo Aug 06 '15 at 11:55
  • wow. `dataType : "json"` solved it ... I assumed hat this specifies the data-type passed to PHP and not how to handle the return. Thank You so much! – grueb Aug 06 '15 at 12:03
  • @grueb by default `$.ajax` handle responses based on the MIME type, but you can force it with this parameter. – Hacketo Aug 06 '15 at 12:09
  • @Hacketo Please check the edit history of the question – Tushar Aug 06 '15 at 15:23
  • @Tushar I saw it, and OP wanted to do the JSON.parse inside the promise, he already did the async callback stuff – Hacketo Aug 06 '15 at 15:49

0 Answers0