您好,我正在编写 PHP 脚本,我遇到一个问题,当我从浏览器打开 URL 时,它会成功打开,但当尝试发出 ajax 请求时,会出现 404 错误。我的代码如下
jQuery.ajax({
url : "http://example.com/ajaxRequest/index.php",
method : "POST",
cache: false,
data : {
name : "Mohsin",
},
success : function(data){
alert(data);
},
});
相同的 URL 在浏览器中打开,但使用 ajax 请求时出现 404 错误。我还包括了
header('Access-Control-Allow-Origin: *');
在上面的脚本中
最佳答案
在你的ajax中提及跨域请求。并且还可以使用 jsonp
进行跨域请求。
url : "http://example.com/ajaxRequest/index.php",
method : "POST",
cache: false,
data : {
name : "Mohsin",
},
crossDomain:true, <---- add this line
dataType : 'jsonp', // also this line
success : function(data){
alert(data);
},
关于javascript - URL 在浏览器中工作正常,但使用 ajax 请求时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729439/