【问题标题】:Why is jQuery ajax get returning a 404 Not found error even while the file exists on the server?为什么即使文件存在于服务器上,jQuery ajax 也会返回 404 Not found 错误?
【发布时间】:2013-07-07 21:07:10
【问题描述】:

当我通过浏览器访问以下网址时,它可以正常返回 JSON 数据,

http://azcvoices.com/topcompanies/wp-content/themes/topcompanies/get.php?p=33

当 jquery 执行 ajax get 时,即使文件 get.php 如上所述确实存在于服务器上,它也会失败并使用以下代码生成 404 Not found error

$.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() {  });

您可能会看到下面的 404 错误,

目前 .htaccess 包含以下内容,

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

这会导致问题吗?

测试服务器上的相同演示, http://peplamb.com/workspace/azcentral.com/spotlight-stories/ 工作正常,但相同的代码在 http://azcvoices.com/topcompanies/spotlight-stories/

失败

可能是什么问题?非常感谢任何帮助!

【问题讨论】:

  • 当我在浏览器中访问该链接时,我得到一个 404...
  • 你能再试一次吗?我正在测试一些东西!

标签: javascript .htaccess jquery http-status-code-404


【解决方案1】:

您是否从与托管页面相同的域发出请求?如果没有,您可能会遇到Cross-Origin Resource Sharing 的问题。

要解决此问题,您可以将Access-Control-Allow-Origin: * 添加为标题。

【讨论】:

  • 我认为它没有生效。当我获取页面时,我没有在标题中看到它。编辑-没关系,现在似乎可以工作了。
  • 嗨,马特,感谢您的快速响应,我添加了 header("Access-Control-Allow-Origin: *"); get.php 文件也继续进行并将 AddType application/json .json 添加到 .htaccess 文件中,但结果仍然相同,知道为什么吗?
  • 现在可以在标题中看到了吗?
  • 感谢马特的提示! header('HTTP/1.1 200 OK', true, 200);成功了!
猜你喜欢
  • 2019-12-09
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多