2017-07-30 35 views
2
$.ajax({ 
     type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
     url   : 'https://your_url.aspx', // the url where we want to POST 
     data  : school, // our data object 
     dataType : 'json', // what type of data do we expect back from the server 
        encode   : true 
    }) 
     // using the done promise callback 
     .done(function(data) { 

      // log data to the console so we can see 
      console.log(data); 

      // here we will handle errors and validation messages 
     }); 

    // stop the form from submitting the normal way and refreshing the page 
    event.preventDefault(); 

这是我的代码,以某种形式的数据上传到特定的网址张贴,但我收到以下错误:访问控制允许来源的错误,而在阿贾克斯

POST https://your_url.aspx 500 (Internal Server Error) 
send @ jquery-3.2.1.min.js:4 
ajax @ jquery-3.2.1.min.js:4 
(anonymous) @ magic.js:20 
dispatch @ jquery-3.2.1.min.js:3 
q.handle @ jquery-3.2.1.min.js:3 
index.html:1 XMLHttpRequest cannot load 
https://your_url.aspx. No 'Access-Control- 
Allow-Origin' header is present on the requested resource. Origin 'null' is 
therefore not allowed access. The response had HTTP status code 500. 

我环顾四周对于如何让访问控制允许来源和$就

beforeSend: function(request){ 
    request.setHeaderRequest("Access-Control-Allow-Origin","*") 
} 

也计算器看到更多的答复后,后来我改变setHeaderRequest到addHeaderReuest使用,但即使如此,我没能看到响应从ser ver,虽然错误已被删除。控制台显示为空白。

任何帮助,将不胜感激。

回答

1

出现此错误是因为您试图从您的域中访问另一个域。使用同源策略,我们只能请求存在于同一个域中的那些url。 跨域请求一般称为CORS只有在我们从其他站点进行身份验证之前,浏览器才允许使用。 访问控制 - 允许来源响应标头指示响应是否可以与具有给定源的资源共享。检查文档Access-Control-Allow-Origin

+0

写crossDomain:true也没有帮助。 –

+0

不,在这里你cleraly获取错误消息作为'允许起源'标题出现在所请求的资源。' –

+0

您想要击中的另一个网站可能不允许跨域请求 –

相关问题