且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有一个JQuery对MVC操作的ajax调用总是返回错误

更新时间:2022-04-15 22:26:52

您期望(返回)操作方法中的string值.为什么需要将数据类型指定为json呢?删除它,看看会发生什么.而且响应中没有 d 属性!因此只需在警报中使用结果.

You are expecting (returning) a string value from your action method. Why do you need to specify the datatype as json then ? Remove that and see what happens. And there is no d property from the response ! so just use result in the alert.

$.ajax({
         type: "POST",
         url: "/OrderDetail/LoadModelData",
         contentType:"application/json; charset=utf-8",         
         data: JSON.stringify({ 
                             id: docId, 
                             filename: fileName, 
                             description: fileDescription 
                            }),
         success: function (result) {
         alert("ok: "+ result);
         },
         error: function (result) {
             alert('Oh no: '+ result.responseText);
         }
     });

datatype属性告诉服务器客户端期望返回什么样的内容.

the datatype property tells the server that what kind of content the client is expecting back as the result.

如达林所说,请使用JSON.stringify方法构建JSON请求.更新此答案,为将来的访问者提供正确的方法.

EDIT : As Darin mentioned, Please Use the JSON.stringify method to build the JSON request. Updating this answer to include correct way for future visitors.