16

我收到以下错误:jquery ajax readystate 0 responsetext status 0 statustext error当给它时: url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm),但是当我url(localhost:""/embparse_page)在我的本地主机上给它时它工作正常。

我尝试使用在 Google 搜索中找到的标题,我也使用beforeSend:""过,但它仍然无法正常工作。

我认为主要问题是:XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.但我不明白。

任何人都可以向我解释这个问题,因为我对此很陌生。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta Access-Control-Allow-Origin="*" />
    <title>Page Parsing</title>
    <script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
    <script>
    getit=function(){
        jQuery.support.cors = true;
        $.ajax({
            type:"GET",
            url:"http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm",
            dataType:"html",
            crossDomain:true,
            beforeSend: function(xhr) {
                xhr.overrideMimeType('text/plain;charset=UTF-8');
            },
            success:function(XMLHttpRequest,jqXHR ,data) {
                //alert(data.title);
                var starttitl=data.lastIndexOf('<title>');
                var endtitl=data.lastIndexOf('</title>');
                var title1=data.substring(starttitl+7,endtitl);
                alert(title1);
            },
            error:function(errorStatus,xhr) {
                alert("Error"+JSON.stringify(errorStatus));
            }
        });
    }   
    </script>
</head>
<body>
    <div id="siteloader">
        <input type="button" onclick="getit()" />
    </div>
</body>
</html>
4

5 回答 5

21

我收到了这个错误,在我的情况下,这不是由于同源政策。我从这个链接得到了一些帮助。其他可能的原因可以在这里看到。

我的情况是,我有一个链接按钮,但我没有使用 e.PreventDefault()

ASPX

<asp:LinkButton ID="lnkSearch" runat="server" CssClass="DockCmdSearch" CommandName="Search" OnClientClick="return VerifySearch(this, event);" />

Javascript

function VerifySearch(sender, e) {        
    e.preventDefault();
    $.ajax({
                type: 'POST',
    .............
    }
    return false;
}
于 2014-03-07T00:09:43.407 回答
0

同源政策。当您打开时浏览器不允许

http://site1.com

连接到:

site2.com
sub.site1.com
site1:99.com
https://site1.com (not sure about this one)

这样 site1 就不能从 site2 窃取内容并假装它是 site1 的内容。解决这个问题的方法是 JSONP(我认为使用谷歌地图)并site2提供 cors 标头,但 jQuery 1.* 不支持 cors(可能也不在 2.* 中),因为 IE 在实现它时存在一些问题。在这两种情况下,您都需要 site2 与您的站点合作,以便您的站点可以显示其内容。

如果您只自己使用它,那么您可以使用 Firefox 并安装 forcecors 插件。要激活,您可以选择视图 => 工具栏 => 添加栏,然后单击屏幕右下角的文本“cors”。

于 2013-10-16T05:42:08.453 回答
0

我从我的 Ajax 调用中得到了那个错误,而为我解决这个错误的只是输入了“return false”。

于 2018-01-28T21:31:22.457 回答
0

我对 Nginx(服务器端)和 AngularJs(用户端)有同样的问题,
因为其他开发人员说它的 Cors 问题,在这里我只想说我如何解决我的问题,也许有人使用这种方法;)
首先我在我的下面添加了以下行Nginx 配置文件(在 linux -> /etc/nginx/sites-available/your domain):

    add_header Access-Control-Allow-Origin *;
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

并使用 angularJs 我这样发送我的请求:

        $http({
            method: "POST",
            data: { //params},
            url: "//address",
            headers: { //if its form data
                "Accept": "application/json",
                "Content-Type": "application/x-www-form-urlencoded",
                "Cache-Control": "no-cache"
            },
            crossDomain: true,
            contentType: false,
            processData: false
        }).then(function mySucces(response) {
            alert(JSON.stringify(response));
        }, function myError(response) {
            alert("error");
        });

于 2018-08-04T06:13:06.753 回答
-3

我正在测试读取数据txt/XML文件json/xml并收到错误...读取的值:Status[0] & readyState[0]并且StatusText[error];这在 Internet Explorer 上成功运行,但在 Chrome 上无法正常运行,因为域需要相同

这就是修复它的原因

转到 C:\WINDOWS\system32\drivers\etc\hosts

为您的本地主机应用程序命名:

127.0.0.1   SampleSiteName.com

现在在 chrome 中打开代码http://SampleSiteName.com/YourAjaxFileName.htm(如果它打开,则表示您已正确输入主机名)转到您的HTML文件并提供您尝试读取的文件的相对地址(如果FileToBeRead.txtYourAjaxFileName.htm,然后只需输入网址:“ /FileToBeRead.txt”)

现在您的代码也可以在 Chrome 上运行。

于 2016-10-21T04:47:19.950 回答