1

I have a form with a textarea and when trying to send the information, I am using JSON.stringify, I get errors. Here is the code:

        $.ajax({
            type: "POST",
            dataType: "json",
            data: JSON.stringify({
                reportid: data["reportid"], //Guid
                assessmentId: data["assessmentId"], //Guid
                title: data["title"], //string
                body: data["body"], //string
                section: data["section"], //string
                reportsection: data["reportSection"] //string
            }),
            url: "/Assessments/ModifyAssessmentTemplateText",
            success: function (data) {
                lastModified = data["LastModified"];
                updateLastModified(lastModified);
                alert(data);
            }
        });

My controller method was set up as follows:

[POST("ModifyAssessmentTemplateText")]
[AjaxOnly]
public JsonResult ModifyAssessmentTemplateText(Guid reportid, Guid assessmentid, string title, string body, string section, string reportSection)
{
//...
}

I get a 500 server error.

I know that when I tried testing, and had only one parameter, reportid, and had my method accept a string, it worked. But when I set it to Guid I get the 500 error. Is there a way I should be parsing the JSON server side?

EDIT:

Note: when I don't use data, and I do url: "/Assessments/ModifyAssessmentTemplateText?reportid=" + reportid

it works with no problem.

2
  • Try the parameter as nullable (Guid? reportid, Guid? assessmentid... Commented May 7, 2013 at 20:19
  • The Guid though is never null. Commented May 7, 2013 at 20:20

5 Answers 5

1

I think the answers so far are dancing around the point. Your data doesn't have to be a string at all, have you tried this?

$.ajax({
    type: "POST",
    dataType: "json",
    data: {
        reportid: data["reportid"], //Guid
        assessmentId: data["assessmentId"], //Guid
        title: data["title"], //string
        body: data["body"], //string
        section: data["section"], //string
        reportsection: data["reportSection"] //string
    },
    url: "/Assessments/ModifyAssessmentTemplateText",
    success: function (data) {
        lastModified = data["LastModified"];
        updateLastModified(lastModified);
        alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to call JSON.stringify, because you are expecting parameters so it should be key value pairs and you are trying to POST JSON object.

You may crate parameter that matches JavaScript object and then use JSON.stringify, this way model binder will try to deserialize it into the object.

5 Comments

I need to do stringify for when I send a string that has newlines and special characters though.
jQuery will convert it into proper key value pairs and encode the content. Stringify converts JavaScript object into JSON string literal representation.
So shouldnt $.ajax({ type: "POST", dataType: "json", data: { reportid: data["reportid"] }, contentType: "application/json; charset=utf-8", url: "/Assessments/ModifyAssessmentTemplateText", success: function (data) { lastModified = data["LastModified"]; updateLastModified(lastModified); } }); work?
The error from ELMAH is: System.ArgumentException: Invalid JSON primitive: reportid.
Of course because it can not bind JSON string to any of your paramters. dataType:'json', refers to data type returned by your server. Leave everything as is just remove JSON.stringify.
1

So I managed to find the answer on http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/. Essentially, what I needed to do was encapsulate the JSON string in quotation marks. This made the string a JSON string rather than a JavaScript Object literal.

Quoted from the site:

The solution is as simple as two single-quotes:
// RIGHT
$.ajax({
  type: 'POST',
  contentType: 'application/json',
  dataType: 'json',
  url: 'WebService.asmx/Hello',
  data: '{ FirstName: "Dave", LastName: "Ward" }'
});

Did you spot the difference? Instead of a JavaScript object literal, the data parameter is a JSON string now. The difference is subtle, but helpful to understand. Since it’s a string, jQuery won’t attempt to perform any further transformation, and the JSON string will be unimpeded as it is passed to the ASMX ScriptService.

2 Comments

But this is the same as his sample, since JSON.Stringify converts object into JSON string.
When I did JSON.stringify though for some reason it didnt work, not sure why.
0

Have you tried posting the guid as a string and parsing out a Guid on the server?

Comments

0

If you trying to send data as json. You should know:

Specifying the Data Type for AJAX Requests

json - Evaluates the response as JSON and sends a JavaScript Object to the success callback.

But how binder detect that it is json request?

By contentType: 'application/json; charset=utf-8'

3 Comments

Thanks for the tip. However, even after adding it all it did not help. $.ajax({ type: "POST", dataType: "json", data: { reportid: data["reportid"] }, contentType: "application/json; charset=utf-8", url: "/Assessments/ModifyAssessmentTemplateText", success: function (data) { lastModified = data["LastModified"]; updateLastModified(lastModified); } });
@pendraggon87 Create model object for posted data and send all fields. And then show us error message.
I am trying to avoid the overhead of creating an object model just for this. It seems strange that if I can send it through the url it works, but if I do it through body (data) it does not. The GUIDs are necessary for the function to do a couple lookups and determine if it should perform a modification

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.