douke8473 2015-07-23 08:22
浏览 54
已采纳

JQuery ajax永远不会成功。 这是什么错误?

I'm trying to make an ajax to a php-server that use Wordpress. I'm using the rewrite rule to redirect the ajax calls:

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

add_action('init','Ajax_roules_setup');

And this is working fine (if i go to localhost/wordpress/ajax/myrequest i get the page with the request).

Now I have this php code that manage the ajax request:

<?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));
?>

As said befor it works!! An this is the js code (use 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);
                                }
                            });
                        }
                    }
            );
        }
);

So for what i have understand this should work, but all the calls that are made end with error 404 not found! Can be a problem of Permalink, or is something else?

  • 写回答

1条回答 默认 最新

  • doutuoji8418 2015-07-23 08:24
    关注

    It is success not succes

    try :-

    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);
                                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单