Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从json字符串获取数据_Jquery_Json - Fatal编程技术网

Jquery 从json字符串获取数据

Jquery 从json字符串获取数据,jquery,json,Jquery,Json,我在从我的JSONP字符串中获取值时遇到了一个问题,我通过AJAX请求获取该字符串,如下所示: result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"4"},"message":"","result":[{"ID":"1","user_email":"admin@domain.com","custom_fields":{"us

我在从我的JSONP字符串中获取值时遇到了一个问题,我通过AJAX请求获取该字符串,如下所示:

 result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"4"},"message":"","result":[{"ID":"1","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.5081","user_latitude":"0.0878"}},{"ID":"2","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.9081","user_latitude":"0.2878"}},{"ID":"3","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.6081","user_latitude":"0.1878"}}]})
我的javascript应该采用user_lationuser_longitue的值,但由于某些原因无法实现

 var myData = JSON.parse(jsonString);

 $(document).ready(function () {

var distanceObj = [],
    i = 0;

$.each(myData.result, function (a, b) {
    distanceObj[i] = { distance: hesapla(51.41140000, -0.22600000, b.custom_fields.user_longitude, b.custom_fields.user_latitude), location: a };
    ++i;
});

distanceObj.sort(function(a,b) {
    return parseInt(a.distance) - parseInt(b.distance)
});

$.each(distanceObj, function(a, b) {
    $('#groups').append('<li>' + b.location + ': ' + b.distance + 'm</li>');
});

console.log(distanceObj);

function hesapla(meineLongitude, meineLatitude, long1, lat1) {
    erdRadius = 6371;

    meineLongitude = meineLongitude * (Math.PI / 180);
    meineLatitude = meineLatitude * (Math.PI / 180);
    long1 = long1 * (Math.PI / 180);
    lat1 = lat1 * (Math.PI / 180);

    x0 = meineLongitude * erdRadius * Math.cos(meineLatitude);
    y0 = meineLatitude * erdRadius;

    x1 = long1 * erdRadius * Math.cos(lat1);
    y1 = lat1 * erdRadius;

    dx = x0 - x1;
    dy = y0 - y1;

    d = Math.sqrt((dx * dx) + (dy * dy));


    return Math.round(d * 1000);
};

});

您的AJAX调用有几个问题。清理这些数据应该可以让您正确地获取数据

$.ajax({
    url: 'mydomain.com/api',

    // You should never use `async: false`
    // P.S. JSONP adds a `<script>` tag, so this does nothing
    //async: false,

    // This will change the request to `mydomain.com/api?callback=result`
    // instead of `mydomain.com/api?callback=<someRandomFunctionName>`
    // You can get rid of this if you set your server to
    // use `$_GET['callback']` when it creates its response
    jsonpCallback: 'result',

    // `contentType` is the `Content-type` of the REQUEST body,
    // it does nothing here
    //contentType: 'application/json; charset=utf-8',

    // jsonp is a "hack" to get around the same-origin policy
    // it adds a `<script>` tag to the DOM to get the data
    // the response should be a function call to the "callback"
    // with the data you want to return
    dataType: 'jsonp',

    timeout: 2000,

    success: function (data) {
        // This is only called on success,
        // so if we're here, we can assume we have the data

        // This is all asynchronous, so you need to put all
        // code that needs access to this inside this callback
        // or use `$.ajax(...).done(function(data){...});`
        $.each(data.result, function(i, v){
            console.log(v.ID);
        });
    }
});
$.ajax({
url:'mydomain.com/api',
//永远不要使用'async:false'`
//P.S.JSONP添加了一个``标记,所以它什么也不做
//async:false,
//这将把请求更改为'mydomain.com/api?callback=result`
//而不是“mydomain.com/api”回调=`
//如果将服务器设置为
//在创建响应时使用“$\u GET['callback']”
jsonpCallback:“结果”,
//`contentType`是请求正文的`Content type`,
//它在这里什么也不做
//contentType:'application/json;charset=utf-8',
//jsonp是绕过同源策略的“黑客”
//它向DOM添加一个``标记以获取数据
//响应应该是对“回调”的函数调用
//使用要返回的数据
数据类型:“jsonp”,
超时时间:2000,
成功:功能(数据){
//这只要求成功,,
//所以如果我们在这里,我们可以假设我们有数据
//这都是异步的,所以您需要将所有
//需要在此回调中访问此的代码
//或者使用`$.ajax(…).done(函数(数据){…})`
$.each(数据、结果、函数(i、v){
控制台日志(v.ID);
});
}
});

这不是json。这是jsonp,你不能
json.parse
那…我有什么办法可以使用这个jsonp吗?你从哪里得到这些数据?从我的服务器上,这实际上是对服务器的ajax请求…你能给我们看一下ajax调用代码吗?您可能只需要将其设置为使用
数据类型:“jsonp”
或其他类型。jQuery将为您处理所有的“解析”,您只需要告诉它如何进行。非常感谢@RocketHazmat
$.ajax({
    url: 'mydomain.com/api',

    // You should never use `async: false`
    // P.S. JSONP adds a `<script>` tag, so this does nothing
    //async: false,

    // This will change the request to `mydomain.com/api?callback=result`
    // instead of `mydomain.com/api?callback=<someRandomFunctionName>`
    // You can get rid of this if you set your server to
    // use `$_GET['callback']` when it creates its response
    jsonpCallback: 'result',

    // `contentType` is the `Content-type` of the REQUEST body,
    // it does nothing here
    //contentType: 'application/json; charset=utf-8',

    // jsonp is a "hack" to get around the same-origin policy
    // it adds a `<script>` tag to the DOM to get the data
    // the response should be a function call to the "callback"
    // with the data you want to return
    dataType: 'jsonp',

    timeout: 2000,

    success: function (data) {
        // This is only called on success,
        // so if we're here, we can assume we have the data

        // This is all asynchronous, so you need to put all
        // code that needs access to this inside this callback
        // or use `$.ajax(...).done(function(data){...});`
        $.each(data.result, function(i, v){
            console.log(v.ID);
        });
    }
});