2013-10-25 3 views
-8

У меня есть простой вызов POST AJAX - как только это закончится, я хочу запустить пользовательскую функцию - я пробовал .success() без всякой радости - может кто-нибудь мне помочь?jQuery - сделайте что-нибудь после завершения AJAX

jQuery.post('http://www.site.com/product/123/', jQuery('#product_addtocart_form').serialize(), function() 
    // on success - do something here 
}); 
+5

В чем проблема? Ответ на ваш вопрос ... Чтобы управлять ошибками, используйте вместо этого '$ .ajax'. Существует уже много ответов в Интернете .......... – JoDev

+4

вам не хватает открывающей фигурной скобки '... '). Serialize(), function()' <--- здесь – Novocaine

+0

@JoDev OP также может использовать ['.fail()'] (http://api.jquery.com/deferred.fail/) как часть обратного вызова отложенного объекта. – War10ck

ответ

2

Вы можете сделать это:

$.post(url, data, function() { 
    alert("success"); 

    // Call the custom function here 
    myFunction(); 
}); 

Или это:

// Assign handlers immediately after making the request, 
// and remember the jqxhr object for this request 
var jqxhr = $.post(url, data); 

jqxhr.done(function() { 
    alert("second success"); 
    // Call the custom function here 
    myFunction(); 
}); 
+1

+1 Для обработки отложенных объектов ... – War10ck

3

Вы можете использовать любые следующие обратные вызовы, основанные на вашем требовании в $.ajax()

.done(function() { 
    alert("success"); 
    }) 
    .fail(function() { 
    alert("error"); 
    }) 
    .always(function() { 
    alert("complete"); 
    }); 
+1

+1 Для обработки отложенных объектов ... – War10ck

1

Попробуйте это вне для вашего ajax звоните:

<script> 
    $.ajax({ 
     type: "POST", 
     url: "./WebServices/MethodName", 
     data: "{someName: someValue}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      var row = response.d; 
      if (row.length > 0) { 
       $.each(row, function (index, item) { 
       }); 
      } 
      else { 
       $("#").html("No Rows Found"); 
      } 
     }, 
     failure: function() { 
     } 
    }); 
</script> 
1

Основы использования .ajax будет выглядеть примерно так:

HTML

<form id="foo"> 
<label for="bar">A bar</label> 
<input id="bar" name="bar" type="text" value="" /> 
<input type="submit" value="Send" /> 
</form> 

<!-- The result of the search will be rendered inside this div --> 
<div id="result"></div> 

JavaScript

/* Attach a submit handler to the form */ 
$("#foo").submit(function(event) { 

/* Stop form from submitting normally */ 
event.preventDefault(); 

/* Clear result div*/ 
$("#result").html(''); 

/* Get some values from elements on the page: */ 
var values = $(this).serialize(); 

/* Send the data using post and put the results in a div */ 
$.ajax({ 
    url: "test.php", 
    type: "post", 
    data: values, 
    success: function(){ 
     alert("success"); 
     $("#result").html('Submitted successfully'); 
    }, 
    error:function(){ 
     alert("failure"); 
     $("#result").html('There is error while submit'); 
    } 
    }); 
}); 
Смежные вопросы