Ajax请求不是从JQuery发送的

时间:2015-03-08 07:40:33

标签: javascript ajax

我使用以下代码发送ajax请求,但请求未发送。任何人都可以帮我找到问题吗?

 $("input.ub").click(function () {
     console.log("I am here");
     var id = $(this).attr('id');
     var pid = "p" + id.substring(1, 2);
     var text = $("#" + pid).text();
     $("#" + pid).html('<input type="text" id="up" value="' + text + '" /><input type="button" id="req" value="Submit" />');

     //Ajax request to be sent
     $("#req").one("click", function () {

         //var action = $("#postform").attr('action');
         console.log("I am Second");
         var form_data1 = {
             post: $("#up").val(),
             is_ajax: 1,
             update: parseInt(id.substring(1, 2))
         };
         console.log($("#up").val());
         $.ajax({
             type: "POST",
             url: "updatePosts.php",
             data: form_data1,
             success: function (response) {
                 if (response == "success") {
                     console.log("Succes MEssage");
                     $(location).attr('href', 'viewPosts.php');
                 } else console.log("You are failed here instead");
             }
         });
         console.log("Request not sent");
         return false;
     });

我收到失败的控制台消息。但我没有在代码中看到任何问题。

2 个答案:

答案 0 :(得分:1)

您正在返回&#34;请求未发送&#34;即使它成功了。您需要在AJAX调用中使用error设置。

http://api.jquery.com/jquery.ajax/

答案 1 :(得分:0)

看起来你可能忘记了一个支架......

           $("input.ub").click(function(){
            console.log("I am here");
            var id = $(this).attr('id');
            var pid="p"+id.substring(1,2);
            var text=$("#"+pid).text();
            $("#"+pid).html('<input type="text" id="up" value="'+text+'" /><input type="button" id="req" value="Submit" />');
            //Ajax request to be sent
            $("#req").one("click",function(){

    //var action = $("#postform").attr('action');
                console.log("I am Second");
    var form_data1 = {
        post: $("#up").val(),
        is_ajax: 1,
                        update:parseInt(id.substring(1,2))
    };  
                console.log($("#up").val());
    $.ajax({
        type: "POST",
        url: "updatePosts.php",
        data: form_data1,
        success: function(response){
            if(response == "success"){ 
                console.log("Succes MEssage");
                $(location).attr('href','viewPosts.php');
            }
            else { <=====HERE
                console.log("You are failed here instead");
            }
    });
    console.log("Request not sent");
    return false;
});