简体   繁体   English

jQuery无法从PHP文件读取JSON

[英]jQuery can't read JSON from PHP file

I've managed to encode Wordpress data as JSON for a storelocator page but the page is failing to parse the JSON returned. 我已经成功将Storepressator页面的Wordpress数据编码为JSON ,但是该页面无法解析返回的JSON It gives a 404 Not Found error in the console even though the file is present and is actually returning JSON data as it should. 即使文件存在并且实际上正在按原样返回JSON数据,它也会在控制台中显示404 Not Found错误。

The storelocator JS (essential code): 商店定位器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);

My PHP: 我的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);

?>

Sample of the JSON returned: 返回的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"}]

Note: When I copy the returned data into a JSON file and use that as the data location it works perfectly. 注意:当我将返回的数据复制到JSON文件并将其用作数据位置时,它可以完美地工作。 I've checked file permissions and everything is fine. 我已经检查了文件权限,一切都很好。

Where could I be going wrong? 我哪里出问题了?

This is not the right way to use AJAX in wordpress, You have to send the request to admin-ajax.php with a action hook. 这不是在wordpress中使用AJAX的正确方法,您必须使用操作钩子将请求发送到admin-ajax.php Take a look on the example: 看一下示例:

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

Now what you have to do, invoke the two hooks, wp_ajax_ and wp_ajax_nopriv_ like this, Go to the functions.php in your themes folder. 现在您要做的是,像这样调用两个钩子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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM