Skip to content Skip to sidebar Skip to footer

Form Post Warning Message After Upgrading Jquery

I have upgraded to jquery 1.10.2. I am using jquery migrate and I am having the warning message 'jQuery.parseJSON requires a valid JSON string' I have not understood how I can cor

Solution 1:

You need to send your data as JSON.

Where you have data: $("#DateFrom").val(), replace it with data: JSON.stringify({$("#DateFrom").val()}).

EDIT: You may need to send it as JSON.stringify({(myModel: $("#DateFrom").val()}).

Solution 2:

The error may be related to "" or false values that used to convert to null in versions of jQuery prior to 1.9.0 per THIS PAGE

Here it the relevant excerpt including the suggested solution:

JQMIGRATE: jQuery.parseJSON requires a valid JSON string

Cause: Before jQuery 1.9.0, the $.parseJSON() method allowed some invalid JSON strings and returned null as a result without throwing an error. This put it at odds with the JSON.parse() method. The two methods are aligned as of 1.9.0 and values such as an empty string are properly not considered valid by $.parseJSON().

Solution: If you want to consider values such as "" or false successful and treat them as null, check for them before calling $.parseJSON(). Since falsy values such as an empty string were previously returned as a null without complaint, this code will suffice in most cases:

var json = $.parseJSON(jsonString || "null"); 

If your own code is not calling $.parseJSON() directly, it is probably using AJAX to retrieve a JSON value from a server that is returning an empty string in the content body rather than a valid JSON response such as null or {}. If it isn't possible to correct the invalid JSON in the server response, you can retrieve the response as text:

$.ajax({
    url: "...",
    dataType: "text",
    success: function( text ) {
        var json = text? $.parseJSON(text) : null;
        ...
    } 
});

Solution 3:

Remove content-type attribute and do like this:

functionSearch() {

        $.ajax({
            cache: false,
            url: "@Url.Action("Search","ControllerName")",
            dataType:"html",
            data:
            {
            myModel: $("#DateFrom").val()
            },
            success: function (data)
            {
                $("#NewDiv").html(data);
            },
            error: function (request, status, error)
            {
                  DisplayError(ParseErrorFromResponse(request.responseText, "Unknown error"), true);
            }
        });

 }

and in action:

public PartialViewResult Search(string myModel)
    {
        return PartialView("SearchResult", myModel);
    }

Solution 4:

I think it's because you're missing double quotes around key myModel in line

...
data:  JSON.stringify({myModel: $("#DateFrom").val()},
...

and not because of jquery version upgrade.try using

data:  JSON.stringify({"myModel": $("#DateFrom").val()},

Solution 5:

Try like this.. You have to return Json as result from your Search().

 public JsonResult Search(myModel myModel)
 {
    return Json(result);
 }

and also in you Script, try like this..

$.ajax({
        type:"POST"cache: false,
        contentType: "application/json; charset=utf-8",
        url: "@Url.Action("Search","ControllerName")",
        dataType: "json",
        data:  JSON.stringify({"myModel": $("#DateFrom").val()}),
        success: function (data)
        {
            $("#NewDiv").html(data);
        }
    });

Also check $("#DateFrom").val() is having correct value and put an

alert(data)

in success and check the returned data.

I have updated the url.

Post a Comment for "Form Post Warning Message After Upgrading Jquery"