我正在尝试为使用Wordpress的php服务器制作ajax。
我正在使用重写规则来重定向ajax调用:

function Ajax_roules_setup(){
        add_rewrite_rule(
            'ajax/([^/]*)',
            'index.php?action=$matches[1]',
            'top'
        );
}


add_action('init','Ajax_roules_setup');

并且这工作正常(如果我转到localhost / wordpress / ajax / myrequest,则获得带有请求的页面)。

现在,我有管理ajax请求的以下php代码:

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function load_template_part($template_name, $part_name=null) {
    ob_start();
    get_template_part($template_name, $part_name);
    $var = ob_get_contents();
    ob_end_clean();
    return $var;
}

$reply = null;
switch ($wp->query_vars['action']){
    case 'get-more-posts':
        $html_for_ajax = sprintf('%s',  load_template_part("content","fourFrom"));
        $reply =
            array(
                'status' => 'success',
                'last'   => ''.$all_post_loaded,
                'html'   => $html_for_ajax ,
            );
        break;
}

print_r(json_encode($reply));
?>


如前所述,它有效!
这是js代码(使用jQuery):

var calls = 1;
jQuery(document).ready(
        function () {
            jQuery("#forntPage-getMore").click(
                    function () {
                        if (calls >= 0) {
                            jQuery.ajax({
                                // definisco il tipo della chiamata
                                type: "POST",
                                // specifico la URL della risorsa da contattare
                                url: "http://localhost/wordpress/ajax/get-more-posts",
                                // passo dei dati alla risorsa remota
                                data: "ajax_get_more=" + calls,
                                // definisco il formato della risposta
                                dataType: "json",
                                // imposto un'azione per il caso di successo
                                succes: function (result) {
                                    //var reply = jQuery.parseJSON(result);
                                    if (reply.last) {
                                        calls = -1;
                                        jQuery("#forntPage-getMore").remove;
                                    } else {
                                        calls++;
                                    }
                                    jQuery("#frontpage-post-grid").append(reply.html);
                                }, error: function (xhr, status, error) {
                                    var err = eval("(" + xhr.responseText + ")");
                                    alert(err.Message);
                                }
                            });
                        }
                    }
            );
        }
);


因此,对于我所了解的内容,这应该可以正常工作,但是找不到以错误404结尾的所有调用!
可能是固定链接的问题,还是其他问题?

最佳答案

success不是succes

尝试:-

success: function (result) {
                                    //var reply = jQuery.parseJSON(result);
                                    if (reply.last) {
                                        calls = -1;
                                        jQuery("#forntPage-getMore").remove;
                                    } else {
                                        calls++;
                                    }
                                    jQuery("#frontpage-post-grid").append(reply.html);
                                }, error: function (xhr, status, error) {
                                    var err = eval("(" + xhr.responseText + ")");
                                    alert(err.Message);
                                }

关于javascript - jQuery ajax永远不会成功。怎么了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31581886/

10-11 12:56
查看更多