【问题标题】:Wordpress admin-ajax.php returns php code instead of jsonWordpress admin-ajax.php 返回 php 代码而不是 json
【发布时间】:2016-04-26 07:00:30
【问题描述】:

如何让 WordPress 在 Nginx 上返回 JSON?

在我的 wordpress 中,Ajax 调用返回 <?php JSON 而只是简单的 JSON。 该站点运行良好,除了 admin-ajax 调用。

定期通话

文件上传调用

技术信息:

Wordpress 4.5
PHP 5.6.11-1ubuntu3.1 (fpm-fcgi)
nginx version: nginx/1.9.3 (Ubuntu)

标题嗅探:

Request URL:http://SERVER/wp-admin/admin-ajax.php
Request Method:POST
Status Code:200 OK
Remote Address:IP:80
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://SERVER
Cache-Control:no-cache, must-revalidate, max-age=0
Connection:keep-alive
Content-Type:application/json; charset=UTF-8
Date:Tue, 26 Apr 2016 06:44:17 GMT
Expires:Wed, 11 Jan 1984 05:00:00 GMT
Pragma:no-cache
Server:nginx/1.9.3 (Ubuntu)
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Robots-Tag:noindex
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,pl;q=0.6
Connection:keep-alive
Content-Length:107
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:REDACTED ;)
Host:SERVER
Origin:http://SERVER
Referer:http://SERWER/wp-admin/media-new.php
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
data[wp-auth-check]:true
interval:60
_nonce:32b36eaa97
action:heartbeat
screen_id:media
has_focus:false

NGINX

location / {
    index index.php index.html index.htm;
            try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

【问题讨论】:

  • 您的代码中似乎有 extre
  • WP 安装是全新的。我会尝试@Fiil 的答案...

标签: php json ajax wordpress nginx


【解决方案1】:

只是为了结束这个话题。 问题是functions.php文件中没有评论 我在functions.php添加以下内容后,问题就消失了。

<?php
/**
 * Twenty Fifteen functions and definitions
 *
 * Set up the theme and provides some helper functions, which are used in the
 * theme as custom template tags. Others are attached to action and filter
 * hooks in WordPress to change core functionality.
 *
 * When using a child theme you can override certain functions (those wrapped
 * in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before
 * the parent theme's file, so the child theme functions would be used.
 *
 * @link https://codex.wordpress.org/Theme_Development
 * @link https://codex.wordpress.org/Child_Themes
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * {@link https://codex.wordpress.org/Plugin_API}
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

【讨论】:

  • 是的没错,但是一般来说,出现这个问题是因为&lt;?php标签后面没有空行
【解决方案2】:

检查您的 wordpress 文件是否未被泄露。您可能在核心 Wordpress 文件中进行了一些修改,添加了“

重新安装 Wordpress 文件并检查它是否仍然存在。 对 wordpress 的所有修改都应该在主题文件夹或插件中完成。

我的问题仍然存在尝试禁用所有插件并一一打开。有可能是其中一个插件搞砸了。

【讨论】:

  • 完全全新安装,我删除了所有插件,仍然一样:(
  • 看来你的nginx配置有问题。非 AJAX 请求的原始响应如何?
  • 非 ajax 完美运行。 WP 工作,我可以编辑帖子并显示它们.. 它只是 AJAX 返回&lt;?phpJSON
  • 您是否尝试过全新安装 Wordpress(新文件夹、新数据库、默认主题、无插件)?非 AJAX 响应是否也以
  • 是的,我做到了。我完全删除了所有文件并上传了新文件。从插件中删除所有内容,在数据库中附加一个新表(无法创建新数据库)。依然没有。检查文件权限,nginx“修复路径”设置,php版本,标题,文件所有者......我迷路了:(
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2017-12-21
相关资源
最近更新 更多