XMLHttpRequest 无法使用 jQuery 加载 URL [英] XMLHttpRequest cannot load an URL with jQuery

查看:37
本文介绍了XMLHttpRequest 无法使用 jQuery 加载 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从远程"网站获取一些 json 数据.我在 99000 端口上运行我的 Web 服务,然后在 99001 端口 (http://localhost:99001/index.html) 上启动我的网站.

I'm trying to get some json data from a "remote" website. I run my web service on the 99000 port then, I launch my website on the 99001 port (http://localhost:99001/index.html).

我收到以下消息:

    XMLHttpRequest cannot load http://localhost:99000/Services.svc/ReturnPersons. Origin http://localhost:99001 is not allowed by Access-Control-Allow-Origin.

即使我以 HTML 文件的形式启动我的网页,我也会得到这个:

Even If I launch my web page as an HTML file, I get this:

    XMLHttpRequest cannot load http://localhost:99000/Services.svc/ReturnPersons.Origin null is not allowed by Access-Control-Allow-Origin.

Web 服务返回数据.我尝试捕捉这样的数据项:

The web service returns data. I try to catch the data items like this:

var url = "http://localhost:99000/Services.svc/ReturnPersons";
$.getJSON(url, function (data) {
success: readData(data)
});
function readData(data) {
    alert(data[0].FirstName);
}

我正在尝试获得这种结构:

And I'm trying to get this structure:

[{"FirstName":"Foo","LastName":"Bar"},{"Hello":"Foo","LastName":"World"}]

你知道我为什么会收到这个错误吗?

Do you know why I'm getting this error?

推荐答案

你不能做一个 XMLHttpRequest 跨域,唯一的选项"是一种叫做 JSONP 的技术,归结为:

You can't do a XMLHttpRequest crossdomain, the only "option" would be a technique called JSONP, which comes down to this:

开始请求:添加一个带有远程 url 的新

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