javascript 为什么即使文件存在于服务器上,jQuery ajax 也会返回 404 Not found 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17562122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is jQuery ajax get returning a 404 Not found error even while the file exists on the server?
提问by Matt Bryant
When I access the following url via browser it works fine returning JSON data,
当我通过浏览器访问以下 url 时,它可以正常返回 JSON 数据,
http://azcvoices.com/topcompanies/wp-content/themes/topcompanies/get.php?p=33
http://azcvoices.com/topcompanies/wp-content/themes/topcompanies/get.php?p=33
When jquery does an ajax get
it is failing with a 404 Not found error
with the following code even when the file get.php
truly exists on the server as mentioned above,
当 jquery 执行 ajax时,即使文件确实存在于如上所述的服务器上,get
它也会失败并404 Not found error
显示以下代码get.php
,
$.ajax(
{
url: "http://azcvoices.com/topcompanies/wp-content/themes/topcompanies/get.php",
type: "GET",
data: {p: postId}
})
.done(function(post) {
})
.fail(function() { alert("error"); })
.always(function() { });
You may see the 404 error below,
您可能会看到下面的 404 错误,
Currently the .htaccess has the following in it,
目前 .htaccess 中有以下内容,
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file= [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ [L]
RewriteRule . index.php [L]
Could this be causing the issue?
这可能是导致问题的原因吗?
The same demo on the test server at, http://peplamb.com/workspace/azcentral.com/spotlight-stories/works fine, but the same code is failing at http://azcvoices.com/topcompanies/spotlight-stories/
测试服务器上的相同演示, http://peplamb.com/workspace/azcentral.com/spotlight-stories/工作正常,但相同的代码在http://azcvoices.com/topcompanies/spotlight-stories上失败/
What could be the issue? Any help is greatly appreciated!
可能是什么问题?任何帮助是极大的赞赏!
回答by Matt Bryant
Are you making the request from the same domain as the page is hosted on? If not, you might be running into a problem with Cross-Origin Resource Sharing.
您是否从与托管页面的域相同的域发出请求?如果没有,您可能会遇到跨域资源共享问题。
To fix this, you might be able to add Access-Control-Allow-Origin: *
as a header.
要解决此问题,您可以添加Access-Control-Allow-Origin: *
为标题。