我已经阅读了SO并尝试了我可以在网上找到的所有内容,但在为不同的域执行jQM AJAX请求时,我仍然遇到以下错误:
XMLHttpRequest cannot load http://a.b.c/search.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
jQM代码很简单:
$( document ).on( "mobileinit", function()
{
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
$(document).on('vclick', '#btnConfirmOrder', function(event)
{
$.ajax({url: baseUrl + 'order.php',
data: {"shoppingCart" : shoppingCart, "userId":1},
type: 'post',
async: 'true',
dataType: 'json',
beforeSend: function()
{
$.mobile.loading('show', {theme:"a", text:"wait...", textonly:true, textVisible: true});
},
complete: function() {
$.mobile.loading('hide');
},
success: function (result)
{
showMsg("success.");
},
error: function(jqXHR, textStatus, errorThrown)
{
//#err
console.log((jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
showMsg("error.");
}
});
});
这是nginx conf(也试过了http://enable-cors.org/server_nginx.html)
server {
listen 80;
server_name a.b.c;
root /etc/nginx/html/ws2;
location / {
index index.html index.htm;
add_header Access-Control-Allow-Origin: *;
add_header Access-Control-Allow-Methods "GET,POST,OPTIONS";
add_header Access-Control-Allow-Headers Content-Type;
add_header Access-Control-Max-Age 86400;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#cors
location ~* \.(eot|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
}