为什么除 IE 之外的所有浏览器上的 XMLHttpRequest.status == 0? [英] Why is XMLHttpRequest.status == 0 on all the browsers except IE?

查看:78
本文介绍了为什么除 IE 之外的所有浏览器上的 XMLHttpRequest.status == 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个很疯狂...

我用 C# 制作了一个 RESTful.NET 网络服务,并作为独立服务运行.当我使用 XMLHttpRequest 来检索 JSON 数据时,除 IE 之外的所有浏览器都失败了,因为 XMLHttpRequest 实例的状态标志始终为 0——它应该是 200,这就是 IE 发生的情况.

I made a RESTful.NET webservice with C# and is running as a standalone service. When I make a XMLHttpRequest to retrieve JSON data, all browsers fail, except IE, because the status flag of the instance of XMLHttpRequest is always 0 -- it should be 200, that it's what's happening with IE.

我已经读到 0 是不在网络服务器上的静态 html 页面的正确值.所以,我安装了 Apache 并将页面放在那里,但结果是一样的(我可能错误地解释了建议......)

I have already read that 0 is the correct value for a static html page who are not on a webserver. So, I installed Apache and put the page there, but the result is the same (I may have wrongly interpreted the suggestion...)

我运行了 Fiddler,对于任何浏览器,请求总是正确检索!

I ran Fiddler and the requests are always correctly retrieved for any browser!

这是我正在使用的 JavaScript 代码:

Here's the JavaScript code that I'm using:

<script type="text/javascript">

var serviceURI = 'http://localhost:8889/WebService/client/25';
var xmlHttp = new XMLHttpRequest();

function clientHandler()
{
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        var json = xmlHttp.responseText;
        var client_id = document.getElementById('clientid');
        var client_name = document.getElementById('clientname');
        var client_address = document.getElementById('clientaddress');
        var client_phone = document.getElementById('clientphone');

        var client = eval('(' + xmlHttp.responseText + ')');
        client_id.value = client.id;
        client_name.value = client.name;
        client_address.value = client.address;
        client_phone.value = client.phoneNumber;
    }
    else
        alert("Error:\nxmlHttp.responseText = " + xmlHttp.responseText + "\nstatus = " + xmlHttp.status);
}

function sendRequest()
{
    if (xmlHttp)
    {
        xmlHttp.onreadystatechange = clientHandler;
        xmlHttp.open('GET', serviceURI, true);
        xmlHttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        xmlHttp.send(null);
    }
    else
        alert('xmlHttp not defined!');
}
</script>

如果有人能向我解释发生了什么,我会非常高兴;我还没有为此找到任何满意的解释...

I would be really pleased if someone could explain me what's happening; I haven't found any sactisfatory explnation for this...

另外,对于这项工作,使用像 jQuery 这样的框架会更好吗?

Also, is it better to use a framework like jQuery for this job?

谢谢大家!

PS:我已经咨询过 Jon Flanders RESTful.NET,但它对解决这个问题没有多大帮助... :(

PS: I've have consulted Jon Flanders RESTful.NET, but it ain't helping much on solving this... :(

我将这个问题的答案移到下面的评论中!

I moved the answer to this question to a comment bellow!

推荐答案

目前的问题与同源策略有关.我在 StackOverflow 上找到了 2 篇文章帮助我确定了问题:

The present problem is related with the same origin policy. I've found 2 articles on StackOverflow that helped me to identify the problem:

问题的解决方法是这里!我在服务器的方法中添加了以下行:

The solution for the problem is here! I added the following line to the method of the server:

WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

通过为每个方法添加这一行,响应的标头如下:

By adding this line for each method, the response's header comes as follow:

HTTP/1.1 200 OK
Content-Length: 81
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/1.0
Access-Control-Allow-Origin: *
Date: Sun, 31 Oct 2010 02:34:34 GMT

<json data>

通过这种方式,该服务可以在 Firefox、Safari、Chrome 和 IE 上运行.但是,正如在答案链接上所说,必须有更好的方法来解决这个问题.我会尝试找到相关的东西:)

This way the service works on Firefox, Safari, Chrome and IE. But, as is said on the link with the answer, there must be a better way to solve this. I'll try to find something about that :)

这篇关于为什么除 IE 之外的所有浏览器上的 XMLHttpRequest.status == 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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