gpt4 book ai didi

jquery - parsererror 语法错误 : Unexpected token < - Load Partial View using jQuery Ajax in ASP. NET MVC 4

转载 作者:行者123 更新时间:2023-12-03 22:28:29 29 4
gpt4 key购买 nike

我使用以下 Ajax 调用将部分 View 加载到 div 中:

  $.ajax({
url: "/URL",
type: "POST",
dataType: "json",
data: JSON.stringify(request),
contentType: "application/json; charset=utf-8",
success: function(data) {
$('#Result').html(data);
App.hidePleaseWait();
},
error: function (jqXHR, textStatus, errorThrown) {
App.hidePleaseWait();
alert(textStatus);
alert(errorThrown);
}
});

这是我的 Controller :

[HttpPost]
public ActionResult GetSomething(MyModel formModel)
{
var model = new ResultModel();

try
{
model.Data = GetSomeData();
}
catch (Exception exception)
{
model.ErrorMessage = exception.Message;
}

return PartialView("_Results", model);
}

我收到以下错误 "parserrror SyntaxError: Unexpected token <"

似乎 .ajax 调用期望返回 json 而不是 html。我需要做什么来解决这个问题?

谢谢。

最佳答案

您需要更改 ajax 调用中的数据类型。

 dataType: "json",

 dataType: "html", 

Datatype 告诉类型是 json,但是您发回的部分 View 是 html。因此它尝试将其解析为 json 数据并抛出错误。

数据类型 - 您期望从服务器返回的数据类型。

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

关于jquery - parsererror 语法错误 : Unexpected token < - Load Partial View using jQuery Ajax in ASP. NET MVC 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18882705/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com