问题描述
这是我的代码
$.ajax(
{
type: "GET",
url: 'http://devserver:7995/stdpart/services/GetAllPartsWithFilter',
dataType: 'json',
data: jsonPartsData,
success: fnGetPartsData,
error: PartsLoadError
});
这是代码在 IE8 中运行良好,但在 Firefox 和 Chrome 浏览器中运行失败.当我检查 XHR 对象时,它表示状态代码为 0.我检查了所有其他问题,但没有一个问题能帮助我识别问题.
This is code working fine in IE8, But getting failed in Firefox and Chrome browsers. When i, inspect the XHR object, it's saying the status code code is 0. I have checked all other questions, none of them are helped me to identify the issue.
如果我在这段代码中做错了什么,请告诉我.如果 $.ajax 有一些兼容性问题,那么请提出与之等效的建议.
Let me know, if i am doing any thing wrong in this code. If $.ajax has some compatibility issues, then please suggest something equivalent to it.
更新:我们在以下位置找到了一种解决方案http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html
Update: We found one solution at http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html
它使用了动态脚本的概念.我们在我们的应用程序中做了同样的事情,然后每件事现在似乎都在工作.还是要全面分析.
It is using the concept of Dynamic Scripting. We have done the same thing in our application, then every thing seems to be working now. Yet to analyze fully.
推荐答案
这是因为 同源政策.您不能使用 ajax 调用外部站点.如果你真的想使用,你必须使用 JSONP.或者您可以为此使用服务器端代理.意思是,在服务器端调用外部站点并对那个 web 服务进行 ajax 调用.
this is because of the Same origin policy. you cannot use ajax to call external sites. if you really want to use, you have to use JSONP. Or you can use serverside proxy for this. means, call external site in the server side and do ajax call to the that webservice.
更新:
在您的网站中创建 webserviceice,并在 webmethod 中输入以下代码
create webserveice in your site and in the webmethod put following code
string proxyURL = "http://devserver:7995/stdpart/services/GetAllPartsWithFilter";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode.ToString().ToLower() == "ok")
{
Stream content = response.GetResponseStream();
StreamReader contentReader = new StreamReader(content);
return contentReader.ReadToEnd();
}
return string.Empty;
然后使用您的代码访问本地服务.
then access local service using your code.
更多信息请参考这个链接
这篇关于$.ajax 调用在 IE8 中运行良好,但在 Firefox 和 chrome 浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!