Ajax call with jQuery in ASP.NET MVC does not pass parameters 



حمایت از ما | مشهدهاست

Ajax call with jQuery in ASP.NET MVC does not pass parameters


حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست
`

Ajax call with jQuery in ASP.NET MVC does not pass parameters



asked

6 years ago

viewed

7733 times

active

6 years ago


The Route is:

routes.MapRoute(     "Ajax", // Route name     "BizTalk/Services/{action}", // URL with parameters     new     { // Parameter defaults      controller = "BizTalk"     }    ); 

My Controller is:

public JsonResult AjaxTest(string s, int i, bool b)   {    return Json("S: " + s + "," + "I: " + i + "," + "B: " + b);   } 

My jQuery Code:

$(document).ready(function() {    $("#btn_test").click(function() {     var s = "test";     var i = 8;     var b = true;     $.ajax({      type: "POST", cache: false,      url: "/BizTalk/Services/AjaxTest",      data: { i: i, s: s, b: b },      contentType: "application/json; charset=utf-8",      dataType: "json",      success: function(msg) {      }     });    });   }); 

2010-05-07 01:37:57Z


1:


This post explains the problem and a possible solution (similar to how @Erv has explained).

If you remove contentType: "application/json; charset=utf-8" from your call to jQuery.ajax the default content type (form-urlencoded) will be used and the json data you have specified as your data parameter (data: { i: i, s: s, b: b }) will be mapped correctly to your action parameters....so unless you really want to send json data just remove the contentType and you will be fine.....



2:


ASP.NET MVC does not automatically map incoming JSON data into action method parameters.

See the following article for a solution to add that capability:

http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

Also, I think your jQuery call does not actually send JSON although that seems to be what you want it to do. jQuery will not automatically convert the data object to JSON. You need to convert it to JSON yourself with something like json2.js



3:


how did you get that "jquery ... does not pass parameters"? have you tried to sniff the request with firebug?

you're sending data in POST body but trying to access them in regular way (with using action arguments) like GET.
all POST data is in Request.Form or you have to handle it by binding to some custom ViewModel.



4:


Erm, might be wrong but you are passing in the jQuery i, s, b but in the action you have s, i,b .

The order must be correct for jQuery posts.

EDIT

Here is how I use jQuery posts;

jQuery

        $.post("/Articles/jQueryAddComment", { commentText: commentText, id: id, type: commentType }, function(returnedHTML) { //Do something with the returned html.         }); 

In my controller

        public ActionResult jQueryAddComment(string commentText, int id, string type)         { //do some stuff                 return PartialView("CommentList", fvm);         } 



 اين مطلب بدون دخالت انساني عينا از اين آدرس کپي شده و تمامي مسوليت آن با ناشر اصلي است.

Why is access to the path denied?
Login failed for user 'IIS APPPOOL\ASP.NET v4.0'
How to call javascript function from code-behind
ASP.NET Web Application Message Box
IE9 JavaScript error: SCRIPT5007: Unable to get value of the property 'ui': object is null or undefined
How to Convert DataSet to DataTable

Response.Redirect to new window
Call a stored procedure with parameter in c#
An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode
How to get current page URL in MVC 3
Read data from SqlDataReader
Get current date only in C#
*