获取响应代码并通过jQuery ajax解析消息

时间:2015-06-02 21:17:56

标签: javascript jquery ajax

我有这个结构: HTML:

<input type="submit" name="_ninja_forms_field_7" class="ninja-forms-field  popup-submit" id="ninja_forms_field_7" value="" rel="7">

JS:

$('#ninja_forms_field_7').click(function () {
    var name = $('#ninja_forms_field_6').val();
    var surname = $('#ninja_forms_field_6').val();
    var emailAddress = $('#ninja_forms_field_8').val();
    var eCommerceSiteUrl = $('#ninja_forms_field_9').val();




    var post_datas = emailAddress = +emailAddress+ & name = +name+ & surname = +surname+ & eCommerceSiteUrl = +eCommerceSiteUrl;
    $.ajax({
        type: 'POST',
        url: 'myserviceaddress',
        data: post_datas,
        success: function (answer) {
            console.log(answer);
        }
    });
});

它运作良好。但我想,如果服务响应代码为0,则解析页面中的文本。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

只需添加error: function()下面的success行:

$.ajax({
    type: 'POST',
    url: 'myserviceaddress',
    data: post_datas,
    success: function (answer) {
        console.log(answer);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        $("#result_div").html("textStatus");
    }
});

另外,请确保创建<div id="result_div"></div>,以便JQuery可以放置结果。