I'm using the following Ajax call to load a partial view into a div:
我正在使用以下Ajax调用将部分视图加载到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);
}
});
Here's my 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);
}
I get the following error "parserrror SyntaxError: Unexpected token <"
我收到以下错误“parserrror SyntaxError:Unexpected token <”
It seems that the .ajax call is expecting json back instead of html. What do I need to do to fix this?
似乎.ajax调用期待json而不是html。我需要做些什么来解决这个问题?
Thanks.
谢谢。
2 个解决方案
#1
27
You need to change your datatype in the ajax call.
您需要在ajax调用中更改数据类型。
dataType: "json",
to
至
dataType: "html",
Datatype tells that type is json, but you send back the partial view which is html. So it tries to parse it as json data and throws the error.
数据类型告诉该类型是json,但是您发回了部分视图,即html。所以它试图将其解析为json数据并抛出错误。
datatype - type of data you expect back from the server.
datatype - 您希望从服务器返回的数据类型。
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:
dataType(默认值:Intelligent Guess(xml,json,script或html))类型:String您希望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:
#2
1
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:
正如您现在知道的那样,将dataType添加到html而不是json。我还检查了jquery的ajax函数的成功/完成部分中的以下内容:
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:
此外,有时您可能需要将从服务器收到的数据解析为html,如下所示:
var htmldata = $.parseHTML(data); //where data is the stuff you got from server. and now use this htmldata for your processing.
parseHTML()的参考
#1
27
You need to change your datatype in the ajax call.
您需要在ajax调用中更改数据类型。
dataType: "json",
to
至
dataType: "html",
Datatype tells that type is json, but you send back the partial view which is html. So it tries to parse it as json data and throws the error.
数据类型告诉该类型是json,但是您发回了部分视图,即html。所以它试图将其解析为json数据并抛出错误。
datatype - type of data you expect back from the server.
datatype - 您希望从服务器返回的数据类型。
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:
dataType(默认值:Intelligent Guess(xml,json,script或html))类型:String您希望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:
#2
1
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:
正如您现在知道的那样,将dataType添加到html而不是json。我还检查了jquery的ajax函数的成功/完成部分中的以下内容:
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:
此外,有时您可能需要将从服务器收到的数据解析为html,如下所示:
var htmldata = $.parseHTML(data); //where data is the stuff you got from server. and now use this htmldata for your processing.
parseHTML()的参考