parserrror SyntaxError: Unexpected token < - Load Partial View using jQuery Ajax in ASP.NET MVC 4

前端 未结 2 646
耶瑟儿~
耶瑟儿~ 2021-02-09 01:16

I\'m using the following Ajax call to load a partial view into a div:

  $.ajax({
    url: \"/URL\",
    type: \"POST\",
    dataType: \"json\",
    data: JSON.st         


        
2条回答
  •  独厮守ぢ
    2021-02-09 01:58

    As you now know to add dataType to html instead of json. I also check for the following in the success/done part of jquery's ajax function:

    success: function (response, status, xhr) {
      var ct = xhr.getResponseHeader("content-type") || "";
      if (ct.indexOf('html') > -1) {
        // returned result is of type html, so act accordingly
        }
      else if (ct.indexOf('json') > -1) {
        // returned result is of type json, so act accordingly
      }
    }
    

    Also, there are times when you might need to parse the data you have received from server as html like this:

    var htmldata = $.parseHTML(data); //where data is the stuff you got from server. and now use this htmldata for your processing.
    

    Reference for parseHTML()

提交回复
热议问题