Form warning message after jquery update

I updated to jQuery 1.10.2. I use jquery migrate and I get a warning message "jQuery.parseJSON requires a valid JSON string"

I did not understand how I can fix this. Can someone help me with a better solution, how can I remove the warning message

javascript is as follows:

 function Search() {

        $.ajax({
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "html",
            url: "@Url.Action("Search")",
            data:  JSON.stringify({myModel: $("#DateFrom").val()}),
            success: function (data)
            {
                $("#NewDiv").html(data);
            },
            error: function (request, status, error)
            {
                  DisplayError(ParseErrorFromResponse(request.responseText, "Unknown error"), true);
            }
        });

 }

In the controller:

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

ParseErrorFromResponse:

Function ParseErrorFromResponse(responseText, defaultError)
{
    var text = responseText.replace("<title>", "TitleStart");       
    var startIndex = text.indexOf("TitleStart");
    var endIndex = text.indexOf("TitleEnd");
    return (startIndex == -1 || endIndex == -1) ? defaultError : text.substring(startIndex + 10, endIndex);
}
+4
source share
5 answers

You need to send your details as JSON.

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

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

+3

"" false, null jQuery 1.9.0

, :

JQMIGRATE: jQuery.parseJSON JSON

: jQuery 1.9.0 $.parseJSON() JSON null , . JSON.parse(). 1.9.0, , , $.parseJSON().

. , "" false null, $.parseJSON(). , , , :

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

$.parseJSON() , , AJAX JSON , JSON, null {}. JSON , :

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

content-type :

function Search() {

        $.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);
            }
        });

 }

:

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

, myModel

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

not due to jQuery upgrade.try version using

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

Try it. You should return Json as a result of the search ().

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

as well as in 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 () has the correct value and puts

alert(data)

and check the returned data.

I updated the url.

0
source

Source: https://habr.com/ru/post/1546095/


All Articles