0

Is it possible to post with jquery JSON object and a string parameter to MVC3 controller method ?

This is my function in the controller :

public void myFunction(List<myObject> obj, string myStringparameter)

And my javascript function (without string parameter) :

$.ajax({
    url: "../MyController/myFunction,
    type: "POST",
    processData: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ obj: tabJsonObject})
}).success(function (data) {

    //TODO

})
.error(function (response, status, xhr) {
    //TODO
});

Without string parameter, it works correctly but I don't know how add this string parameter.

I can't add directly in the url because the URL is too long and I think that add this string in JSON array is bad.

Thanks for your help

4
  • It is possible, post json in request body and the string parameter as url parameter, for example, path parameter or query parameter. Commented Dec 1, 2015 at 14:12
  • In fact I found the solution by adding only string parameter in url : Commented Dec 1, 2015 at 14:15
  • I don't understand, what is wrong with data: JSON.stringify({ obj: tabJsonObject, myStringparameter: somestring}) ? If 'myFunction' server side is waiting for a list and a string it should be ok. Commented Dec 1, 2015 at 14:15
  • @user1069516 that's what Im talking Commented Dec 1, 2015 at 14:26

1 Answer 1

1

In fact I found the solution by adding only string parameter in url :

$.ajax({
    url: "../MyController/myFunction?myStringparameter=" + myString,
    type: "POST",
    processData: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ obj: tabJsonObject})
Sign up to request clarification or add additional context in comments.

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.