简体   繁体   中英

Undefined responseText in jsonp request

I 'm using $.ajax to send a jsonp request to an api prepared by a server on https.

My script is as follows:

var sUrl = "https://localhost/api/" + document.getElementById('query').value +
           "?apikey=c13eb63d-b9ee-4ceb-89fe-944072deddbb&fmt=json&limit=&query=" + document.getElementById('query').value;

    $.ajax({
        type: 'GET',
        url: sUrl,
        dataType: 'jsonp',
        jsonp: false,
        async:false,
        crossDomain : true,
        complete: function(jqXHR, textStatus){
                alert(jqXHR);
            alert(jqXHR.responseText);
            }
            });

My request will be send and I can receive the result in my browser and this is my response body:

{"relatedid_restriction":null,"purpose":"mitigation","asn":null,"rir":null,"alternativeid":null,"cc":null,"detecttime":"2014-04-08T17:34:31Z","address":"google.com","alternativeid_restriction":null,"id":"2bdd4e07-26ba-4dc2-99a8-bdea54bf3d4f","guid":"everyone","severity":null,"assessment":"search","rdata":null,"description":"search google.com","asn_desc":null,"relatedid":null,"reporttime":"2014-04-08T17:34:31Z","confidence":"50","restriction":"private","prefix":null} {"relatedid_restriction":null,"purpose":"mitigation","asn":null,"rir":null,"alternativeid":null,"cc":null,"detecttime":"2014-04-08T17:35:14Z","address":"google.com","alternativeid_restriction":null,"id":"1df23bea-ab15-4410-aab4-d2c538a385fd","guid":"everyone","severity":null,"assessment":"search","rdata":null,"description":"search google.com","asn_desc":null,"relatedid":null,"reporttime":"2014-04-08T17:35:14Z","confidence":"50","restriction":"private","prefix":null}

Now the first alert "alert(jqXHR);" returns [object Object] but the second one "alert(jqXHR.responseText);" returns undefined.

How can I retreive the response body text in my javascript?

Thanks

If you use jsonp you need to make a function call. Something like this:

echo $_GET['callback'] . "(" . json_encode($DATA) . ");";

because a jsonp will need to call a function. I think jQuery will add a GET variable with callback as the name.

Than you can do this:

jsonp: "callback",
success : function(data){
    console.log(data);
}

More info and examples: http://www.jquery4u.com/json/jsonp-examples/

From jquery documentation:

The text and xml types return the data with no processing. The data is simply passed on to the success handler, either through the responseText or responseXML property of the jqXHR object, respectively.

That is, responseText is only valid when datatype="text" , so either change datatype if you don't need jsonp or use first parameter in your complete callback, which should be object representing your json response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM