且构网

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

将数据发送到使用JSON MVC控制器

更新时间:2023-02-25 17:16:42

没有理由,如果你问我一个参数转换成JSON。而不是仅仅做到这一点:

There is no reason to convert a single parameter into a JSON if you ask me. Instead just do this:

$.ajax({
    type: 'POST',
    url: "/IM/GetMessages?fUserId=" + idno,
    dataType: 'json',            
    success: function (data) { /*CODE*/
});

这样你仍然可以拿回JSON,但你传递一个参数值。现在,如果你真的需要发送的对象我看不出什么问题,你的code。您可能要声明一个JavaScript变量,并把它变成这样一个JSON对象:

This way you can still get back JSON but you pass single parameter value. Now if you really need to send an object I don't see anything wrong with your code. You might want to declare a javascript variable and turn it into a json object like this:

var myVar = { fUserId: idno };

,然后用它在你的Ajax请求:

and then use that in your ajax request:

$.ajax({
    type: 'POST',
    url: "/IM/GetMessages",
    contentType: 'application/json',
    dataType: 'json',            
    data: JSON.stringify(myVar), 
    success: function (data) { /*CODE*/

});

我这样做,每天和它工作正常,我既可空和非可空类型...

I do this daily and it works fine for me with both nullable and non-nullable types...