简体   繁体   中英

Cross-domain AJAX call returning string JSON, instead of JSON object

I am making a cross-domain AJAX call, and I am not sure if I am doing something wrong or the providers of the API call is incorrectly returning the JSON. Whenever I get the response from the API call, it is a string instead of a JSON object. Here is my AJAX call.

    $.ajax({
        async: false,
        dataType: 'jsonp',
        url: 'http://cross-domain/getSummaryStat.action',
        data: { minDailyDate: start_param, maxDailyDate: end_param },
        success: function(response) {
            map = {
                gamefuse: response["ROM-GF-Live"],
                facebook: response["ROM-FB-Live"],
                kongregate: response["ROM-Kongregate-Live"],
                yahoo: response["ROM-Yahoo-Live"]
            }
        },
        error: function(xhr, textStatus, errorThrown){
           alert('request failed');
        }
    });

When the response comes back, here is response.result

"[{"dayRetention1":"0.01453800063053","visit":"601","installs":"203"},{"dayRetention1":"0.122484891199019","visit":"33863","installs":"10949"]"

NOTE: I set dataType to jsonp because it is a cross-domain AJAX call, and I was getting an error without it.

First, It looks like the returned string isn't even in correct JSON form. It's missing a close bracket at the end.

If this doesn't fix it then the issue here is probably on the server side. Since JSONP is JSON with padding, your return function shouldn't be:

function_name("the string that I return");

Instead you should have:

function_name({
    "name":"Bob Loblaw", 
    "age":40
});

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