gpt4 book ai didi

jquery.post() 从 MVC 4 WebAPI 返回 200 OK,但错误处理程序被命中

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

我遇到了一个奇怪的情况,我的 jquery.post 返回代码 #200(正常),但错误处理程序被命中。

这是我的 MVC WebAPI 服务器端代码(它所做的只是返回代码 #200 OK):

[HttpPost]
public HttpResponseMessage Post(T input)
{
//_handler.Create(input);

return Request.CreateResponse(HttpStatusCode.OK);
}

这是我的客户端脚本

ServerAccess.prototype.Save = function (m) {

$.post("http://localhost:55482/M/", m)
.success(
function (o) {
alert("success");
})
.error(
function (xhr, textStatus, errorThrown) {
if (typeof console == 'object' && typeof console.log == 'function') {
console.log(xhr);
console.log(textStatus);
console.log(errorThrown);
}
}
);
};

Firebug 中的控制台输出为:

POST http://localhost:55482/M/ 200 OK 894ms

Headers


Response Headers

Content-Length 6

Content-Type application/json; charset=utf-8

Date Sun, 28 Oct 2012 18:55:17 GMT

Server Microsoft-HTTPAPI/2.0


Request Headers

Accept /

Accept-Encoding gzip, deflate

Accept-Language nb-no,nb;q=0.9,no-no;q=0.8,no;q=0.6,nn-no;q=0.5,nn;q=0.4,en-us;q=0.3,en;q=0.1

Connection keep-alive

Content-Length 21

Content-Type application/x-www-form-urlencoded; charset=UTF-8

Host localhost:55482

Origin null

User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0

Object { readyState=0, status=0, statusText="error"}

error

(an empty string)

我浏览了一下,发现其他人也有类似的问题,通常是响应中的 JSON 格式错误。由于我只返回 200 OK 且无内容,这似乎不适合我的情况。不过我确实尝试像这样返回一个空对象

var o = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new T());

return Request.CreateResponse(HttpStatusCode.OK, o);

服务器端从 Visual Studio 2012 运行,客户端只是一个带有一些 .js 文件的独立 .html。另外,通过 VS 中的断点,我知道服务器收到了请求,并且使用 Chrome 的 Postman 一切都很好。

所以问题当然是:我在这里做错了什么?为什么会命中错误处理程序?

谢谢!

最佳答案

试试这个:

ServerAccess.prototype.Save = function (m) {
$.ajax({
"type": "POST",
"url": "http://localhost:55482/M/",
"data": m,
"dataType": "json",
"success": function (data, textStatus, xhr) {
alert("success");
},
"error": function (xhr, textStatus, errorThrown) {
if (typeof console === 'object' && typeof console.log === 'function') {
console.log(xhr);
console.log(textStatus);
console.log(errorThrown);
}
}
});
};

这允许您将预期返回 MIME 类型设置为 application/json .

更新

如果您在 http://localhost:55482 上运行独立 HTML 页面,那么就不会跨域了。如果您在 file:///C:/Users/.../Desktop/test.htm 上运行它或者其他地方,那么这将是一个跨域请求。 httphttps:也有区别。

一个jsonp请求是 GET请求且不能是 POST 。这是因为jsonp请求是通过添加 <script> 来实现的元素到页面,源设置为您的 url, data附加到查询字符串(加上时间戳参数)。任何POST期望 jsonp 的请求return 应自动转换为 GET请求。

最后,由于您的例程需要返回数据,因此您需要返回一些实际数据,因为 jQuery 将尝试解析返回到对象中的 JSON(如果没有返回数据,则无法执行此操作)并会抛出错误如果无法解析数据。

尝试将您的回复消息更改为:

String o = "success";
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();

return Request.CreateResponse(HttpStatusCode.OK, ser.Serialize(o));

关于jquery.post() 从 MVC 4 WebAPI 返回 200 OK,但错误处理程序被命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112261/

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