为什么我的 ajax 请求得到响应 0? [英] Why is my ajax request getting response 0?

查看:34
本文介绍了为什么我的 ajax 请求得到响应 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 wp 主题中设置了基本的 wordpress ajax 示例.触发器由 Modernizr.js 检查页面上的媒体查询生成.

jQuery(document).ready(function($) {if(Modernizr.mq('only all and (max-width:6300px)')) {变量数据 = {动作:'my_action',无论如何:ajax_object.we_value//我们以不同的方式传递 php 值!};//我们也可以将 url 值与 ajaxurl 分开传递,用于前端 AJAX 实现jQuery.post(ajax_object.ajax_url, 数据, 函数(数据) {$("#trending-Container").html(data).fadeIn(1000);});}});//结束函数

我已经对我的脚本进行了本地化和排队.

wp_enqueue_script('mainJS', get_template_directory_uri() .'/js/mainJS.js', array("jquery") );wp_localize_script('mainJS', 'ajax_object',数组('ajax_url' => admin_url('admin-ajax.php'),'we_value' => 1234));

最后处理请求的函数是:

add_action('wp_ajax_my_action', 'my_action_callback');add_action('wp_ajax_nopriv_my_action', 'my_action_callback');函数 my_action_callback() {全球 $wpdb;$whatever = intval( $_POST['whatever'] );$无论如何 += 10;回声 $whatever;死();}

这不断给我 0(没有属性)的响应,我不知道为什么.P.S 这都是本地的.

状态码 200主办单位:lart.co.uk来源:http://lart.co.uk参考:http://lart.co.uk/用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36X-Requested-With:XMLHttpRequest表单数据视图源视图 URL 编码动作:我的动作随便:1234

解决方案

这里的一切都必须匹配:

PHP

<前>add_action('wp_ajax_my_action', 'my_action');add_action('wp_ajax_nopriv_my_action', 'my_action');函数 my_action() {}

JS

<前>变量数据 = {动作:'my_action',无论如何:ajax_object.we_value};

此外,您还缺少安全检查和更好的响应处理.
检查以下示例:[ 1 ][2].

I've setup the basic wordpress ajax example in my wp theme. The trigger is made by modernizr.js checking the media queries on the page.

jQuery(document).ready(function($) {
    if(Modernizr.mq('only all and (max-width:6300px)')) {
        var data = {
        action: 'my_action',
        whatever: ajax_object.we_value      // We pass php values differently!
    };
    // We can also pass the url value separately from ajaxurl for front end AJAX implementations
        jQuery.post(ajax_object.ajax_url, data, function(data) {
            $("#trending-Container").html(data).fadeIn(1000);
        });
    }

});//end function 

I have localized and enqueue'd my scripts.

wp_enqueue_script('mainJS', get_template_directory_uri() . '/js/mainJS.js', array("jquery") );
wp_localize_script( 'mainJS', 'ajax_object', 
                    array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );

and finally the function that handles the request is:

add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
function my_action_callback() {
        global $wpdb;
            $whatever = intval( $_POST['whatever'] );
            $whatever += 10;
                echo $whatever;
            die();

        }

This constantly gives me a response of 0 (no properties) and I do not know why. P.S This is all local.

Status code 200
Host:lart.co.uk
Origin:http://lart.co.uk
Referer:http://lart.co.uk/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
action:my_action
whatever:1234

解决方案

Everything has to match here:

PHP

add_action('wp_ajax_my_action',        'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');

function my_action() {}

JS

var data = {
    action: 'my_action',
    whatever: ajax_object.we_value 
};

Also, you're missing security checks and a better handling of the response.
Check this examples: [ 1 ] and [ 2 ].

这篇关于为什么我的 ajax 请求得到响应 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆