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

标签 javascript jquery asp.net ajax

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

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 Internal Server Error

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;
}

最佳答案

在 Update Person C# Method 中,没有“id”参数。要使 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

关于javascript - 在另一个 $.ajax 中调用 $.ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32886036/

相关文章:

javascript - React 16 单选按钮 onChange 不工作

php - 如何从主框架中的 iframe 运行 js 函数?

jquery - 为什么 OS X 上的 FF 在单击事件处理程序中丢失了 jQuery-UI?

.net,Login.aspx,无法显示<img>标签?

JavaScript 持续检查复选框是否被选中

用于防止所有 <a href> 链接的默认值的 Javascript 代码

jQuery : Iterate through all <option> elements in a multiple <select> element

javascript - 获取复选框以仅基于复选框标签内的字符串切换 div - jQuery

asp.net - 母版页是要走的路吗?

javascript - Asp .net core如何加载html文件byte[]到iframe src View