在另一个$ .ajax中调用$ .ajax函数

时间:2015-10-01 11:06:20

标签: javascript jquery asp.net ajax

我有两个$ .ajax函数。第二个函数必须在第一个$ .ajax Success函数中运行。代码在这里:

loadSelectedData = function (id) {
            vex.dialog.buttons.YES.text = 'ویرایش';
            vex.dialog.buttons.NO.text = 'انصراف';

            jQuery.ajax({
                type: "POST",
                url: "LastNewsProfileReport.aspx/GetData",
                data: "{ 'id':'" + id + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                //async: false,
                success: function (data) {
                    vex.dialog.open({
                        message: 'اطلاعات کاربر ' + data.d.FirstName + ' ' + data.d.LastName,
                        input: "<style>\n    .vex-custom-field-wrapper {\n        margin: 1em 0;\n    }\n   .vex-custom-field-wrapper-inline-right {\n        margin: 1em 0;\n   \n        float: right;\n    \n        width: 48%;\n    }\n    .vex-custom-field-wrapper-inline-left {\n        margin: 1em 0;\n   \n        float: left;\n    \n        width: 48%;\n    }\n    .vex-custom-field-wrapper > label {\n        display: inline-block;\n        margin-bottom: .2em;\n    }\n</style>" +
                            "\n<div class=\"vex-custom-field-wrapper\">\n    <label for=\"EmailTxt\">ایمیل</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input name=\"EmailTxt\" type=\"email\" value=\"" + data.d.Email + "\" disabled=\"true\" />\n    </div>\n</div>\n     \n<div class=\"vex-custom-field-wrapper\">\n    <label for=\"UsernameTxt\">نام کاربری</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input name=\"UsernameTxt\" type=\"text\" value=\"" + data.d.UserName + "\" disabled=\"true\" />\n    </div>\n</div>\n     <div class=\"vex-custom-field-wrapper-inline-right\">\n    <label for=\"FirstnameTxt\">نام</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input id=\"FirstnameTxt\" name=\"FirstnameTxt\" type=\"text\" value=\"" + data.d.FirstName + "\" />\n    </div>\n</div>\n<div class=\"vex-custom-field-wrapper-inline-left\">\n    <label for=\"LastnameTxt\">نام خانوادگی</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input id=\"LastnameTxt\" name=\"LastnameTxt\" type=\"text\" value=\"" + data.d.LastName + "\" />\n    </div>\n</div>" +
                            "\n<div class=\"vex-custom-field-wrapper\">\n    <label for=\"NickNameTxt\">نام مستعار</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input id=\"NickNameTxt\" name=\"NickNameTxt\" type=\"text\" value=\"" + data.d.NickName + "\" />\n    </div>\n</div>\n      <div class=\"vex-custom-field-wrapper\">\n    <label for=\"MobileTxt\">شماره همراه</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input id=\"MobileTxt\" name=\"MobileTxt\" type=\"text\" value=\"" + data.d.Mobile + "\" />\n    </div>\n</div>\n      <div class=\"vex-custom-field-wrapper-inline-right\">\n    <label for=\"SexList\">جنسیت</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <select id=\"SexList\" name=\"SexList\" value=\"" + data.d.Sex + "\">  \n<option value=\"" + 0 + "\">لطفا انتخاب نمایید</option> \n<option value=\"" + 1 + "\">مرد</option>    \n<option value=\"" + 2 + "\">زن</option>    </select> \n    </div>\n</div>\n    \n<div class=\"vex-custom-field-wrapper-inline-left\">\n    <label for=\"BirthDateTxt\">تاریخ تولد</label>\n    <div class=\"vex-custom-input-wrapper\">\n        <input id=\"BirthDateTxt\" name=\"BirthDateTxt\" type=\"text\" value=\"" + data.d.Birthdate + "\" />\n    </div>\n</div>\n",
                        afterOpen: function() {
                            jQuery('select option[value=\"' + data.d.Sex + '\"]').attr("selected", true);
                        },
                        callback: function (value) {
                            if (value) {
                                alert('Profile id ' + id + ' Update Successed!');
                                updatePerson(id);
                            } else {
                                return false;
                            }
                        }
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        }

updatePerson函数在这里:

updatePerson = function (id) {
            jQuery.ajax({
                type: "POST",
                url: "LastNewsProfileReport.aspx/UpdatePerson",
                data: {
                personId: '182',
                nickName: 'test',
                firstName:'test',
                lastName: 'test',
                mobileNo: '12345',
                sex: 'men',
                birthDate: 'test'
            },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert('Success');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(textStatus);
                }
            });
        }

第一个$ .ajax函数正常工作;但第二个$ .ajax是500内部服务器错误

C#类代码在这里:

[WebMethod]
public static bool UpdatePerson(string personId, string nickName, string firstName, string lastName, string mobileNo, int sex, string birthDate)
{
    var lastNewsProfileReport = new LastNewsProfileReport();
    lastNewsProfileReport.Update(long.Parse(personId), nickName, firstName, lastName, mobileNo, sex, birthDate);
    return true;
}

1 个答案:

答案 0 :(得分:1)

在更新人员C#方法中,没有&#34; id&#34;参数。对于Ajax调用工作,您必须传递确切的参数名称。

    data: "{ 'id':'182'}", //in update person function javascript

应该像 -

    data: "{'personId': 'value', 'nickName' : 'value', 'firstName' : 'value', 'lastName' : 'value', 'mobileNo' : 'value', 'sex' : 'value', 'birthDate' : 'value'}"

供参考 - 请查看Ajax和.NET上的这篇文章

http://www.cheezycode.com/2015/08/create-html-table-using-jquery-ajax.html

相关问题