简体   繁体   中英

sending data via jQuery to ASP.Net MVC 2 Controller in mixed format

I would like to have an AJAX call with the following type,

$.ajax({
    url:"../..",
    data:{
    stringvar:$("..").val(),
    jsonobj:JSON.stringify({
    }),
    anotherstringvar:$("..").val()
    },
    type:"POST",
    content-type:"application/json"
    success:function(data){
    // do something with the data
    }
 });

How do I achieve this kind of requirement. As you find above, i have to pass normal string values along with the JSON data and i have to bind the same using ASP.NET MVC2 Model binder and JSONValueProviderFactory, which have in place.

You can create a simple JSON object and send it to the server:

var data = JSON.stringify(valuetobestringified);

var json = {
    "json": data,
    "anotherstringvar": $("..").val(),
    "anotherstringvar1": $("..").val()
}

$.post("../..",json,function(){
//response from the server.
});

You can find more information about jQuery here: http://api.jquery.com/jQuery.post/

And you can create a simple JSON object with this online editor: http://jsonlint.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM