Q1: Can somebody provide code example for jquery ajax to server method call in mvc3? Q1a: Does the server return have to be a Json result returned from server side? What are my options with the returned data from the server side method (json, just return a string, etc,)?
-- Something like this...
-- script called from razor view:
$("#textbox1).change(function()
{
$.ajax(
{url: '/controller/action',
data: { val1: $("#text开发者_开发知识库box1").val(), val12: ("#textbox2").val()},
success: function(data){
$("#SumResult").val(data);
}})
});
-- Server method within same controller:
public ActionResult <or> String (int val1, int val2)
{
return (i != null && j != null) ? (i + j).ToString() : "";
}
Below article should give you an idea :
http://tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views
Q1a: Does the server return have to be a Json result returned from server side?
No, you can pretty much return anything from server (in ASP.NET MVC world, from controller method). If you return JSON
, it is pretty much convenient for you to work with it inside your JavaScript code.
What are my options with the returned data from the server side method (json, just return a string, etc,)?
Some of them are :
- XML
- JSON
- Html
Assuming that your controller supposed to return ActionResult
, some of that you can return are as follows :
Content
Json
精彩评论