Javascript jQuery可以';t从PHP文件中读取JSON
我已成功地将storelocator页面的Javascript jQuery可以';t从PHP文件中读取JSON,javascript,php,jquery,json,wordpress,Javascript,Php,Jquery,Json,Wordpress,我已成功地将storelocator页面的Wordpress数据编码为JSON,但该页面无法解析返回的JSON。它在控制台中给出了一个404notfound错误,即使该文件存在并且实际返回了它应该返回的JSON数据 storelocator JS(基本代码): 我的PHP: <?php global $table_prefix, $wpdb, $output_array; require_once('../../../wp-blog-header.php'); require_once
Wordpress
数据编码为JSON
,但该页面无法解析返回的JSON
。它在控制台中给出了一个404notfound
错误,即使该文件存在并且实际返回了它应该返回的JSON
数据
storelocator JS(基本代码):
我的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文件中并将其用作数据位置时,它工作得非常好。我已经检查了文件权限,一切正常
哪里会出错?这不是在wordpress中使用AJAX的正确方法,您必须使用操作挂钩将请求发送到
admin AJAX.php
。看看这个例子:
$.ajax({
url: 'http://localhost/website/wp-admin/admin-ajax.php',
type: 'post',
data: {action: 'my_json_data_fetcher'},
success: function(response){}
});
现在你要做的是,像这样调用两个钩子,wp\u ajax\u
和wp\u ajax\u nopriv\u
,转到主题文件夹中的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);
}
如果在没有AJAX的情况下调用url会发生什么?您会得到一个404错误,这意味着脚本本身发送了一个404头,但它没有发送,或者根本没有执行脚本。总之,我认为您的URL是错误的。@GolezTrol如果我只是在浏览器中访问URL,它会工作得很好,即返回格式良好的URLJSON@pencilsandpixels查看浏览器开发人员工具中的“网络”选项卡。请求是否得到JSON响应?那里的状态码是什么?我们只看到设置了一些配置,而不是实际使用它的功能。你使用的是现成的jQuery插件还是你自己编写的代码?@你可能还需要考虑的像素(我没有看到这已经在你的代码中执行)。另外,如果在利用wp-blog-header.php时直接调用.php文件,您可能首先要定义;否则,Wordpress可能会发现您的请求不是有效的页面请求,并给出您看到的404。确实可以在不使用admin-ajax.php的情况下调用WP功能,但如果我们讨论的是最佳实践,则建议使用admin-ajax.php。
wp\uajax[\unopriv]{action}
方法允许与Wordpress的安全模型进行更紧密的集成,并提供了一个更经得起未来考验的实现。
$.ajax({
url: 'http://localhost/website/wp-admin/admin-ajax.php',
type: 'post',
data: {action: 'my_json_data_fetcher'},
success: function(response){}
});
// 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);
}