简体   繁体   中英

Ajax request not being sent from JQuery

I am using the following code for sending ajax request but the request is not being sent. Can anyone help me to find the problem?

 $("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;
     });

I am getting the failure console messages. But i don't see any problem in the code.

You're returning "request not sent" even when it is successful. You need to use the error setting in your AJAX call.

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

It looks like you may have forgotten a bracket after else...

           $("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;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM