問題描述
我正在嘗試從 Mozilla 獲取使用 REST Web 服務以在 Firefox 3.0.10 下工作的示例代碼.以下代碼在 Firefox 中不起作用,但在 IE 8 中起作用!
I'm trying to get the sample code from Mozilla that consumes a REST web service to work under Firefox 3.0.10. The following code does NOT work in Firefox but does in IE 8!
- 為什么這不起作用?
- IE 8 是否支持 XMLHttpRequest?我見過的大多數例子都使用 ActiveX分配.我應該做什么?XMLHttpRequest 似乎更加標準化.
示例:
var req = new XMLHttpRequest();
req.open('GET', 'http://localhost/myRESTfulService/resource', false); // throws 'undefined' exception
req.send(null);
if(req.status == 0)
dump(req.responseText);
open 語句引發異常,描述為未定義".這很奇怪,因為我分配了 req 對象,在 Firefox 中運行它,并在調用 open 之前檢查以確保它已定義(它說它是對象"類型).
The open statement is throwing an exception with the description 'undefined'. This is strange as I allocate the req object, am running it in Firefox, and checked to make sure it is defined before calling open (which it says it is of type 'object').
我也嘗試過異步版本,但沒有成功.
I've also tried the asynchronous version of this with no luck.
編輯 2:以下是我最近的代碼:
function createRequestObject() {
if( window.XMLHttpRequest ) {
return new XMLHttpRequest();
}
else if( window.ActiveXObject ) {
return new ActiveXObject( "Microsoft.XMLHTTP" );
}
return null;
}
function handleResponse( req ) {
document.writeln( "Handling response..." ); // NEVER GETS CALLED
if( req.readyState == 0 ) {
document.writeln( "UNITIALIZED" );
}
else if( req.readyState == 1 ) {
document.writeln( "LOADING" );
}
else if( req.readyState == 2 ) {
document.writeln( "LOADED" );
}
else if( req.readyState == 3 ) {
document.writeln( "INTERACTIVE" );
}
else if( req.readyState == 4 ) {
document.writeln( "COMPLETE" );
if( req.status == 200 ) {
document.writeln( "SUCCESS" );
}
}
}
document.writeln( "" );
var req = createRequestObject();
try {
document.writeln( "Opening service..." );
req.onreadystatechange = function() { handleResponse( req ); };
req.open('POST', 'http://localhost/test/test2.txt', true); // WORKS IN IE8 & NOT FIREFOX
document.writeln( "Sending service request..." );
req.send('');
document.writeln( "Done" );
}
catch( err ) {
document.writeln( "ERROR: " + err.description );
}
編輯 3: 好的,我在 jQuery 中重新設計了這個.jQuery 在 IE 中運行良好,但從 Firefox 運行時會拋出未定義".我仔細檢查并在 Firefox 中打開了啟用 JavaScript" - 似乎在所有其他網頁中都可以正常工作.下面是jQuery代碼:
EDIT 3: Alright, I reworked this in jQuery. jQuery works great in IE but it throws 'Undefined' when running from Firefox. I double checked and 'Enable JavaScript' is turned on in Firefox - seems to work fine in all other web pages. Below is the jQuery code:
function handleResponse( resp ) {
alert( "Name: " + resp.Name );
alert( "URL: " + resp.URL );
}
$(document).ready( function() {
$("a").click( function(event) {
try {
$.get( "http://localhost/services/ezekielservices/configservice/ezekielservices.svc/test",
"{}",
function(data) { handleResponse( data ); },
"json" );
}
catch( err ) {
alert("'$.get' threw an exception: " + err.description);
}
event.preventDefault();
});
} ); // End 'ready' check
解決方案總結:
好的,網絡課101.我的問題確實是跨域的.我正在查看未發布的網站(僅在文件系統上),該網站正在訪問已發布的服務.當我在同一個域下發布我的網站時,它起作用了.
Alright, web lesson 101. My problem was indeed cross-domain. I was viewing my site unpublished (just on the file system) which was hitting a published service. When I published my site under the same domain it worked.
這也帶來了 IE 和 Firefox 之間的一個重要區別.當IE遇到這種情況時,會提示用戶是否接受跨域調用.Firefox 拋出異常.雖然我可以有一個例外,但更具描述性的例外會有所幫助.
Which also brings up an important distinction between IE and Firefox. When IE experiences this scenario, it prompts the user whether or not they accept the cross-domain call. Firefox throws an exception. While I'm fine with an exception, a more descriptive one would have been helpful.
感謝所有幫助過我的人.
Thanks for all those who helped me.
推薦答案
除非'http://www.mozilla.org/' 是此請求的來源域,由于同源策略,這將不起作用
unless 'http://www.mozilla.org/' is the domain from which this request originates, this wont work because of same origin policy
好的,好的狀態是 200,而不是 0.
edit: Ok, a good status is 200, not 0.
查看 http://dogself.com/telluriumTest/ 并點擊stackoverflow 測試".它使用您的代碼并正常工作.
see http://dogself.com/telluriumTest/ and click on "stackoverflow test". its using your code and working.
特別是這段代碼:
function test(){
var req = new XMLHttpRequest();
req.open('GET', 'index2.htm', false);
req.send(null);
if(req.status == 200)
alert("got some stuff back:"+req.responseText);
}
這篇關于為什么 Mozilla 的這個 XMLHttpRequest 示例在 Firefox 3 中不起作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!