問題描述
這是我的代碼
$.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 瀏覽器中不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!