javascript - jQuery 无法从 PHP 文件读取 JSON

标签 javascript php jquery json wordpress

我已成功将商店定位器页面的 Wordpress 数据编码为 JSON,但该页面无法解析返回的 JSON。即使文件存在并且实际上返回了 JSON 数据,它也会在控制台中给出 404 Not Found 错误。

storelocator JS(基本代码):

var settings = $.extend( {
  'mapDiv': 'map',
  'listDiv': 'loc-list',
  'formContainerDiv': 'form-container',
  'formID': 'user-location',
  'inputID': 'address',
  'dataType': 'json',
  'dataLocation': 'http://localhost/website/wp-content/themes/custom/locations.php',
  'jsonpCallback': null
}, options);

我的 PHP:

<?php

global $table_prefix, $wpdb, $output_array;

require_once('../../../wp-blog-header.php');
require_once('../../../wp-admin/includes/upgrade.php');

$output_array = array();

query_posts('category_name=business&showposts=-1');
//query_posts('name='.$post_name.'&showposts=-1');
while (have_posts()) : the_post();


    $name = get_the_title();
    $summary = get_the_content();
    $lat = get_field( "lat", $post->ID );
    $lng = get_field( "lng", $post->ID );
    $address = get_field( "address", $post->ID );
    $phone = get_field( "phone", $post->ID );
    $web = get_field( "web", $post->ID );

    array_push($output_array, array("id"=>$post->ID,
                    "name"=>$name,
                    "summary"=>$summary,
                    "lat"=>$lat,
                    "lng"=>$lng,
                    "address"=>$address,
                    "phone"=>$phone,
                    "web"=>$web));



endwhile;
//print_r($output_array);
echo json_encode($output_array);

?>

返回的 JSON 示例:

[{"id":76,"name":"AFRICAN ELITE PROPERTIES","summary":"Property development and management","lat":"-33.915025","lng":"18.421118","address":"Somerset Road, Green Point","phone":"021 421 1090","web":"www.africaneliteproperties.com"}]

注意:当我将返回的数据复制到 JSON 文件并将其用作数据位置时,它可以正常工作。我检查了文件权限,一切正常。

我哪里可能出错了?

最佳答案

这不是在 WordPress 中使用 AJAX 的正确方法,您必须使用操作 Hook 将请求发送到 admin-ajax.php。看一下示例:

$.ajax({
    url: 'http://localhost/website/wp-admin/admin-ajax.php', 
    type: 'post', 
    data: {action: 'my_json_data_fetcher'}, 
    success: function(response){}
});

现在你要做的,像这样调用两个钩子(Hook),wp_ajax_wp_ajax_nopriv_,转到你的functions.php主题文件夹。

// should be in your functions.php
// not that my_json_data_fetcher with wp_ajax_ and wp_ajax_nopriv_ hooks
add_action('wp_ajax_my_json_data_fetcher', 'now_your_function_that_return_json');
add_action('wp_ajax_nopriv_my_json_data_fetcher', 'now_your_function_that_return_json');

function now_your_function_that_return_json() {
    global $table_prefix, $wpdb, $output_array;

    $output_array = array();

    query_posts('category_name=business&showposts=-1');

    while (have_posts()) : the_post();


       $name = get_the_title();
       $summary = get_the_content();
       $lat = get_field( "lat", $post->ID );
       $lng = get_field( "lng", $post->ID );
       $address = get_field( "address", $post->ID );
       $phone = get_field( "phone", $post->ID );
       $web = get_field( "web", $post->ID );

       array_push($output_array, array("id"=>$post->ID,
                "name"=>$name,
                "summary"=>$summary,
                "lat"=>$lat,
                "lng"=>$lng,
                "address"=>$address,
                "phone"=>$phone,
                "web"=>$web));



    endwhile;

    echo json_encode($output_array);
    die(0);
}

关于javascript - jQuery 无法从 PHP 文件读取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25723620/

相关文章:

javascript - 在悬停时选择 sibling

php - Laravel 属于/有很多

php - MySQL 选择具有特定条件的分组

php - 防止 IP 欺骗

jquery - CSS溢出:hidden with display:inline-block

javascript - Wiredep 不会插入 bower CSS 依赖项

javascript - 使用 ajax 调用的 while 循环在 javascript 中不起作用

javascript - 主页文本区域更改

jQuery : on check of an option in checkbox show a textbox

php - 在 PHP 中使用主题标签进行重定向的最佳方法是什么?