-8

我有一个简单的 POST AJAX 调用 - 一旦完成,我想运行一个自定义函数 - 我尝试 .success() 没有任何乐趣 - 有人可以帮助我吗?

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

4 回答 4

3

您可以根据您在$.ajax()中的要求使用任何以下回调

 .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
于 2013-10-25T13:02:23.590 回答
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();
});
于 2013-10-25T13:02:59.723 回答
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>
于 2013-10-25T13:04:10.080 回答
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');
    }
  });
});
于 2013-10-25T13:05:16.747 回答