c# - 在这种情况下如何进行ajax?

标签 c# javascript jquery ajax webmethod

JavaScript 代码

  function new_person_adder1() {

                var dataAjax = {
                    'entry_new_person_': $('#entry_new_person_1').val(),
                    'pop_new_person_identity_no': $('#pop_new_person_identity_no1').val(),
                    'identity_type_no': $('#select2_identity_type_bc_for_new_person_1').val()
                };
                jQuery.ajax({
                    url: 'WalkReserve.aspx/quick_person_entry',
                    type: 'POST',
                    data: JSON.stringify(dataAjax),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (data) {
                        console.log(data.d);
                        if (data.d > 0) {
                            alert('Successfully Added');
                        } else {
                            alert('Failed to entry data!');
                        }
                    },
                    error: function (result) {
                        alert('Failed!');
                        console.log('Failed' + result.responseText);
                    }
                });
            }

C#代码

 [WebMethod]
            public static string quick_person_entry(dynamic entry_new_person_, dynamic identity_type_, dynamic pop_new_person_identity_no)
            {
                return Convert.ToString(BAL.pop_quick_person_entry(entry_new_person_, identity_type_, pop_new_person_identity_no));
            }

ajax数据 enter image description here

我收到以下错误

Failed{"Message":"Invalid web service call, missing value for parameter: 
 \u0027identity_type_\u0027.","StackTrace":"   at 
 System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at 
System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at 
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at 
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} 

我想在这里发送 ajax,但由于某些奇怪的原因我无法做到这一点。 我收到以上错误。有人可以指出我的错误或帮助我吗?

最佳答案

查看该方法所需的参数:

entry_new_person_
identity_type_
pop_new_person_identity_no

以及发送的参数:

entry_new_person_
identity_type_no
pop_new_person_identity_no

中间的参数不同。它相似,但不是同一件事。模型绑定(bind)器不够智能(值得庆幸的是)无法匹配仅相似的事物。更改 JavaScript 代码中的 key 应该可以修复该问题:

var dataAjax = {
    'entry_new_person_': $('#entry_new_person_1').val(),
    'pop_new_person_identity_no': $('#pop_new_person_identity_no1').val(),
    'identity_type_': $('#select2_identity_type_bc_for_new_person_1').val()
};

(或者更改方法中的参数名称也可以解决此问题。取决于哪一个更改更大。例如,如果对此方法有大量 JavaScript 调用,那么对该方法进行一次更改会比对方法的调用进行了许多更改。)

关于c# - 在这种情况下如何进行ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24120728/

相关文章:

C# 根据多种类型检查对象类型

javascript - 如何将 Float 转换为 UintArray (IEEE754)?

javascript - 与 Canvas 矩形内的彩色正方形颜色不同的文本

jquery - 修改文本的属性而不是按钮上的 CSS 图标

c# - Application_Error 即使处理异常也会触发

c# - 当 '\' 后跟成为有效转义序列的字符时,在 '\' 字符上拆分字符串

python - 如何将消息连续发送到异步套接字-(Python和C#)

javascript - 内容更改后强制刷新页面

javascript - Timeago + DateJS = NaN

javascript - 当我用 mousemove 旋转图像时,我无法获得负 X 度