我是如何解决这个问题的 [英] How I resolve this problem

查看:71
本文介绍了我是如何解决这个问题的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:XMLHttpRequest无法加载http://google.com/。请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许来源'http:// localhost:50339'访问。响应的HTTP状态代码为405.

 <   !DOCTYPE     html    < span class =code-attribute> PUBLIC     -  // W3C // DTD     XHTML     1.0     Transitional // EN   http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >  
< html xmlns = http://www.w3.org/1999/xhtml >
< head >
< title > < / title >
< script < span class =code-attribute> type = text / javascript src = js / jquery-1.12.3.min.js > < / 脚本 >
< script 类型 = text / javascript src = js / advajax.js > < / script >

< script type = text / javascr ipt >


function doOnlineCheck (){
$ .ajax({
type: POST
url:' http://google.com'
成功: function (data){
alert(' 连接')。
},
错误:功能(数据){
alert(' 无连接。');
}
});
}
< / 脚本 >

< / head >
< 正文 >
< div id = 状态 >
< p id = >
< 输入 类型 = 按钮 / >
< span class =code-keyword><
/ p >
< / div >
< / body >
< / html >





我的尝试:



我的代码是

解决方案

.ajax({
type: POST
url:' http://google.com'
成功: function (data){
alert(' 连接');
},
错误:功能(数据){
alert(' 无连接。');
}
});
}
< / 脚本 >

< / head >
< 正文 >
< div id = 状态 >
< p id = >
< 输入 类型 = 按钮 / >
< span class =code-keyword><
/ p >
< / div >
< / body >
< / html >





我的尝试:



我的代码是


您好,



您正在尝试向应用程序域之外的域发出请求,如果出于安全原因,浏览器阻止这些域(如果不是来自同一来源)并且也是有效的。您正在尝试制作跨域请求。

对于此请求,您的应用程序需要防止CORS,就像POSTMAN一样。

尝试添加

 crossDomain: true 
dataType:' jsonp'



到您的ajax呼叫属性并检查。

我希望工作

您可以点击以下链接:

AJAX Cross Domain |跨源请求| jQuery CORS [ ^ ]



更新:

编辑小提琴 - JSFiddle [ ^ ]



感谢

Error: XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50339' is therefore not allowed access. The response had HTTP status code 405.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="js/jquery-1.12.3.min.js"></script>
<script type="text/javascript" src="js/advajax.js"></script>

    <script type ="text/javascript">

      
        function doOnlineCheck() {
            $.ajax({
                type:"POST",
                url: 'http://google.com',
                success: function (data) {
                    alert('Connection.');
                },
                error: function (data) {
                    alert('No Connection.');
                }
            });
        }
    </script>
    
</head>
<body>
    <div id="status">
        <p id="state">
        <input type="button" />
        </p>
    </div>  
</body>
</html>



What I have tried:

My code is

解决方案

.ajax({ type:"POST", url: 'http://google.com', success: function (data) { alert('Connection.'); }, error: function (data) { alert('No Connection.'); } }); } </script> </head> <body> <div id="status"> <p id="state"> <input type="button" /> </p> </div> </body> </html>



What I have tried:

My code is


Hello,

You are trying to make a request to a domain outside of your application domain, for which the browsers blocks, if not from same origin for security reasons and is valid too. You are trying to make a Cross Domain Request.
For this request, your application needs to be CORS prevented, just like POSTMAN is.
Try adding

crossDomain: true,
   dataType: 'jsonp',


to your ajax call properties and check.
I hope that works
You can follow the below link:
AJAX Cross Domain | Cross-Origin Request | jQuery CORS[^]

Update:
Edit fiddle - JSFiddle[^]

Thanks


这篇关于我是如何解决这个问题的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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