Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax()只是接收到一个错误,而jQuery.post()可以工作_Javascript_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript ajax()只是接收到一个错误,而jQuery.post()可以工作

Javascript ajax()只是接收到一个错误,而jQuery.post()可以工作,javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,争论是把这个放在WordPress.SE上还是放在这里;我决定,既然这主要是一个jQuery问题,我就把它贴在这里,并注意到它发生在WordPress上下文开发过程中 长话短说,下面是一篇关于如何在WordPress中使用AJAX的文章,其中包括粘贴相同的代码摘录,以查看它们在我的开发服务器上是否运行不正常。指定的PHP函数永远不会执行,当我将响应转储到输出div时,调用只返回0 我在AJAX调用中添加了一个错误:字段,该字段被触发,但除了显示有错误之外,我无法找出实际的问题所在 下面我们来看看

争论是把这个放在WordPress.SE上还是放在这里;我决定,既然这主要是一个jQuery问题,我就把它贴在这里,并注意到它发生在WordPress上下文开发过程中

长话短说,下面是一篇关于如何在WordPress中使用AJAX的文章,其中包括粘贴相同的代码摘录,以查看它们在我的开发服务器上是否运行不正常。指定的PHP函数永远不会执行,当我将响应转储到输出div时,调用只返回0

我在AJAX调用中添加了一个
错误:
字段,该字段被触发,但除了显示有错误之外,我无法找出实际的问题所在

下面我们来看看AJAX调用,如指南所示。

jQuery.ajax({
     type : "post",
     dataType : "json",
     url : myAjax.ajaxurl,
     data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
     success: function(response) {
        if(response.type == "success") {
           jQuery("#vote_counter").html(response.vote_count)
        }
        else {
           alert("Your vote could not be added")
        }
     }
  })   
记得以前有过这个问题,我决定回顾一年前的一个老项目,并找到了以下解决方法,它似乎做了同样的事情,但实际上从脚本返回了正确的响应

功能解决方案,实现预期的功能

var ajaxurl = '<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php';
var data = {action: "my_user_vote", post_id : post_id, nonce: nonce};

// Handle any returned values
jQuery.post(ajaxurl, data, function(response) {
    if(response.type == "success") {
       jQuery("#vote_counter").html(response.vote_count)
    }
    else {
       alert("Your vote could not be added")
    }
});
现在发生的是,我们被重定向到一个空白页面,页面上转储了JSON响应。尝试从上面插入.post()方法,出于某种原因,这也在做同样的事情


TL;DR:这可能需要移动到wp.se。

您需要找出实际错误是什么。在浏览器的网络控制台中查找失败的请求,然后查看响应详细信息。确认一下,
myAjax。前者中的ajaxurl
与后者中的
ajaxurl
给出了相同的字符串,不是吗?@Roamer-1888正确。前一种方法使用WordPress的脚本本地化函数来设置值,而后一种方法只是在调用之前直接设置值。如果URL相同,那么唯一明显的区别就是(1)中的显式数据类型。尝试注释数据类型:“json”,。如果没有它,jQuery将做出最佳猜测,正如(2)中所做的那样。Kaji,仅供参考,jQuery的签名。ajax的错误函数是
(jqXHR,textStatus,errorshorn)
。因此,为了更好地了解错误,您可以编写,
jQuery(“#output”).html(textStatus | | errorshorn)
    This post has <div id='vote_counter'>0</div> votes<br>
    <div id="output">Test</div>

    <script>
        jQuery(document).ready( function() {

            jQuery(".user_vote").click( function() {
                post_id = jQuery(this).attr("data-post_id")
                nonce = jQuery(this).attr("data-nonce")

                jQuery.ajax({
                    type : "post",
                    dataType : "json",
                    url : "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php",
                    data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
                    success: function(response) {
                        jQuery("#output").html(response)
                        jQuery("#vote_counter").html(5)
                    },
                    error: function(response) {
                        jQuery("#output").html(response)
                        jQuery("#vote_counter").html(10)}
                })

            })

        })
    </script>
        <?php
    $link = admin_url('admin-ajax.php?action=my_user_vote');
    echo '<a class="user_vote" href="' . $link . '">vote for this article</a>';
function my_user_vote() {
    $result = array(
        'status' => 'success',
        'count' => 5
    );

    $result = json_encode($result);
    echo $result;

    die();
}