0

I have a javascript method which contains an ajax call:

function MyFunction(urlTask, task) {

    ...

    ajax({
        url: urlTask,        
        data: task.Params,
        type: 'POST',
        dataType: 'json'
    })

    ...

}

urlTask is the name of a method in the controller and its name varies depending on some conditions. As a example, sometimes it can be Task1 and sometimes Task2:

    [HttpPost]
    public ActionResult Task1(string Param1, string Param2)
    {
    }

    [HttpPost]
    public ActionResult Task2(string Param1)
    {
    }

Each controller method has a different number of parameters, for example, Task1 has two parameters whereas Task2 has one. So as method in the controller to be called from jquery ajax can vary as well as its parameters I use the above commented scenario, that is, passing the name of the task (urlTask) and task to the javascript function and finally, passing them through the ajax call to the method in the controller.

task.Params contains the necessary parameters to be passed to the controller for each case, either Task1 or Task2. (see below explanation).

What I want is to build the parameters to be passed to the controller method as a string, and then assign it to the 'Data' in the ajax call. task is a json object returned previously by the controller and it contains a field called Params, among others. Params field is a string type which contains the correct parameters depending on the controller method to be called. For example:

task.Params contains a string like below when calling Task1 controller method:

{'Param1':'Param_1','Param2':'Param_2'}

and:

{'Param1':'Param_1'}

when calling Task2 controller method:

My problem is: Controller method, either Task1 or Task2, is receiving its parameters as null so what's wrong? It seems like ajax call is not interpreting correctly the 'data' argument when sending it to the controller method.

Using below in the ajax call is not working:

contentType: 'application/json',
data: JSON.stringify(task.Params)

Any ideas?

1
  • can you show your [WebMethod]? Commented Jan 24, 2014 at 12:55

3 Answers 3

3
 contentType: 'application/json',
 data: JSON.stringify(task.Params)

Worked for me.

My controller:

[Authorize]
[HttpPost]
public ActionResult Test(string Param1, string Param2) {
    return Json(new{ Msg = "Param1: " + Param1 + " & Param2: " + Param2}, JsonRequestBehavior.AllowGet);
}

JS:

          var Task={};
          Task.Params = {'Param1':'Param_1','Param2':'Param_2'};
          $.ajax({
                url: "/Test",
                type: "POST",
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(Task.Params),
                success: function (data) {
                    alert(data.Msg);
                }, error: function (data) {

                }
          });

Output is: {"Msg":"Param1: Param_1 \u0026 Param2: Param_2"}

Sign up to request clarification or add additional context in comments.

6 Comments

use contentType: 'application/json', Edited the ans
The same error, the data received for parameters in the controller method are always nulls...
Cant figure out why.. I ran it in my server and it is working. Posted the sample in my ans. Try and let me know.
Yes, your case is working because you build Task.Params but in my case, Task is an object that it is received as argument in the javascript function and then I access to a string that contains the parameters by doing this: Task.Params. So I think my case is different from yours. For some reason when doing the ajax call this string is not being treated well so the controller method is not receiving the parameters ok (null).
Instead of building Task.Params as you do, imagine this is a string that is received in the javascript method as argument and then you pass it to "data" in the ajax call. Sure it does not work. (This is my case).
|
2

the other way I used in my project:

in javascript:

var sData = "Param1=" + encodeURIComponent(jsParam1) + "&Param2="+ encodeURIComponent(jsParam2);

jQuery.ajax:

    ...
    url: '{server side task1 url}',
    type: 'POST',
    data: sData ,
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    ...

server side:

[HttpPost]
public ActionResult Task1(string Param1, string Param2)
{
    string sUnescapedParam1 = System.Uri.UnescapeDataString(Param1);
    string sUnescapedParam2 = System.Uri.UnescapeDataString(Param2);
}

Comments

0

You can pass the object by converting it as json object like the following syntax

 function MyFunction(urlTask, task) {

           $.ajax({
            url: urlTask,        
            data: "{" + JSON.stringify(task.Params) + "}",
            type: 'POST',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: onSussess,
            error: onError
        });    
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.