jQuery $.getJSON 不工作
我尝试从一台服务器获取 URL 并使用该 URL 获取另一台服务器的内容。
$.ajax({url : 'http://localhost:8080/geturl.jsp?A=1&B=2,C=3',
success : function (data)
{
alert(data);
$.getJSON(data, function (mydata)
{
alert(mydata);
});
},
error : function (data, status, xhr)
{
}
});
我知道我们无法通过ajax调用发出跨域请求,这就是我使用getJSON的原因,我遇到以下问题
- 当我简单地将数据传递到getJSON的url部分(如代码所示)时,警报-框显示正确的 URL,但没有执行任何获取请求(获取请求是从 FireBug 监控的)。
- 当将数据硬编码为“http://www.google.com”时,正在执行获取请求但没有响应,尽管响应标头出现并且响应代码为 200(但它被标记为红色)在 Firebug 中(不知道为什么 :( )
- 当我尝试在 localhost 域中获取网页主机时,尽管响应不是 JSON,但它会正确获取。
我有以下疑问
- 如果 getJSON 函数仅接受 JSON 对象作为响应,那么为什么执行上述 3 时没有出现错误
- 。执行所需功能的正确代码是什么?
- 对每种情况下发生的情况的建议
提前感谢您的答案:)
I am try to get a URL from a one server and using that URL to get contents of another server.
$.ajax({url : 'http://localhost:8080/geturl.jsp?A=1&B=2,C=3',
success : function (data)
{
alert(data);
$.getJSON(data, function (mydata)
{
alert(mydata);
});
},
error : function (data, status, xhr)
{
}
});
I know that we cannot make cross-domain requests in through ajax call, thats why i am using getJSON, i have the following problems
- When i simply pass the data to the url part of getJSON (as shown in the code), the alert-box show the correct URL but no get request is being performed ( get requests were monitored from FireBug).
- When a hard-code the data to be "http://www.google.com" then the get request is being performed but the no response comes, although the response headers comes and response code is 200 (but it was marked as RED in the Firebug (Dont know why :( )
- When I tries to fetch a webpage host in localhost domain, then it is fetched correctly although the response was not JSON.
I have the following doubts
- If the getJSON function accecpts only JSON objects as reponse then why no error came when perform above 3.
- Whats the correct code to perform my the required functionality.
- Suggestions to what happened in each case
Thanks in advance for the answers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getJSON
函数只能跨域使用来获取 JSONP。< br>它不会神奇地规避任何安全限制。
The
getJSON
function can only be used across domains to fetch JSONP.It does not magically evade any security restrictions.
http://api.jquery.com/jQuery.ajax/
这应该是一个工作示例对于 jsonp:
http://api.jquery.com/jQuery.ajax/
This should be a working example for jsonp: