jquery ajax成功结果为null [英] jquery ajax success result is null

查看:308
本文介绍了jquery ajax成功结果为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery进行ajax调用以获取json格式的数据。调用成功回调函数,但数据为空。

I am doing an ajax call using jquery to get data in json format. the success callback function is called but the data is empty.

$(document).ready(function () {
    $.ajax({
        url: "http://apps.sungardhe.com/StudentResearch/public/Research.svc/Schools",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: cbSchools
    });
});

function cbSchools(data) {
    if (data == null) {
        alert("data is null");
        return;
    }
    for (var school in data) {
        $("#ddSchool").append("<option value='" + data[school].ShortName + "'>" + data[school].ShortName + "</option>");
    }
}

使用fiddler我看到响应实际上正在返回json数据但由于某种原因,jquery结果对象为null。谁能告诉我为什么?

using fiddler I see that the response is actually returning the json data but for some reason the jquery result object is null. can anyone tell me why?

推荐答案

你被同源策略,可防止跨域XMLHttpRequests。由于您需要设置标头以从这样的.Net Web服务中获取JSON,因此您处于困境,您无法通过浏览器发出此类请求,而不是来自其他域。

You're being blocked by the same-origin policy which prevents cross-domain XMLHttpRequests. Since you need to set headers to get JSON back from a .Net web service like this, you're in a tough spot, you just can't make that kind of request from a browser, not from a different domain.

Fiddler可能正在显示内容,但浏览器不会让页面看到它,出于安全考虑,它总是为空。解决这个问题的方法是 JSONP ,但不幸的是,它看起来不像是设置以支持它。

Fiddler may be showing the content, but the browser isn't going to let the page see it, for security reasons it'll always be null. The one way around this is JSONP, but unfortunately it doesn't look like that service is setup to support it.

这篇关于jquery ajax成功结果为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆