jmo*_*era 5 jquery json amazon-ec2 asp.net-mvc-3 knockout.js
所以我正在制作一些ajax帖子,它似乎在localhost上正常工作,但是当我将它发布到amazon上的ec2服务器时,我得到Uncaught SyntaxError:意外的令牌B.这似乎指向JSON解析失败.完全相同的数据库,相同的浏览器和相同的方法被调用.为什么它可以在本地而不是服务器上运行.
$.ajax({
url: '@Url.Action("Action")',
type: "POST",
data: ko.toJSON(viewModel),
dataType: "json",
contentType: "application/json; charset:utf-8",
success: function (result) {
},
error: function (xhr, textStatus, errorThrown) {
var errorData = $.parseJSON(xhr.responseText);
var errorMessages = [];
for (var key in errorData)
{
errorMessages.push(errorData[key]);
}
toastr.error(errorMessages.join("<br />"), 'Uh oh');
}
});
Run Code Online (Sandbox Code Playgroud)
这是服务器端的基本布局:
[HttpPost]
public JsonResult Action(ViewModel model)
{
try
{
Response.StatusCode = (int)HttpStatusCode.OK;
return Json("Successfull");
}
catch (Exception ex)
{
logger.Log(LogLevel.Error, string.Format("{0} \n {1}", ex.Message, ex.StackTrace));
Response.StatusCode = (int)HttpStatusCode.BadRequest;
List<string> errors = new List<string>();
errors.Add(ex.Message);
return Json(errors);
}
}
Run Code Online (Sandbox Code Playgroud)
在try语句中,我对数据库进行了几个查询,并在Authorize.Net上发布了一些计算(https://api.authorize.net/soap/v1/Service.asmx)
如果Authorize.net Web服务调用有任何错误,那么我返回如下错误:
if (profile.resultCode == MessageTypeEnum.Error)
{
logger.Log(LogLevel.Error, string.Join(",", profile.messages.Select(x => x.text)));
Response.StatusCode = (int)HttpStatusCode.BadRequest;
List<string> errors = new List<string>();
profile.messages.ToList().ForEach(x => errors.Add(x.text));
db.SaveChanges();
return Json(errors);
}
Run Code Online (Sandbox Code Playgroud)
我正在记录的这个错误:
A public action method 'AddPromoCode' was not found on controller 'Flazingo.Controllers.PositionController'. at
System.Web.Mvc.Controller.HandleUnknownAction(String actionName) at
System.Web.Mvc.Controller.ExecuteCore() at
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at
System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.b__5() at
System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.b__0() at
System.Web.Mvc.MvcHandler.<>c__DisplayClasse.b__d() at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
Run Code Online (Sandbox Code Playgroud)
completedSynchronously)
你有另一个帖子只能在实时服务器上找不到动作,在本地服务器上工作正常,所以我猜这篇文章与javascript片段有关,而不是服务器端片段.
听起来好像服务器上发生了一些不好的事情,服务器发回了某种类型的错误,并且当你试图处理该响应时,你的错误处理程序(在javascript中)就会死掉.
我得到Uncaught SyntaxError:意外的令牌B.这似乎指向JSON解析失败.
这听起来很合理.我们来看看代码:
.ajax({
...
error: function (xhr, textStatus, errorThrown) {
var errorData = $.parseJSON(xhr.responseText);
var errorMessages = [];
...
},
...
});
Run Code Online (Sandbox Code Playgroud)
我强烈建议你看看是什么xhr.responseText.我猜它不包含有效的JSON,因此parseJSON方法抛出'Unexpected token B'错误.
要查看此值,您可以使用console.log(xhr.responseText);或者您可以在Web浏览器或fiddler中使用类似javascript调试器的工具来查看其中的内容.
我的猜测是服务器正在发送一个字符串,There was an error on the server而不是像你期望的那样代替JSON.我看到你内置了错误处理 - 我的猜测是你的错误处理中有一个错误,并没有什么可以捕获它.我建议在服务器端进行调试,以查看某个地方是否存在您不期望的错误.
也许profile.messages只能枚举一次,当你再次尝试时它会抛出一个错误.或者DB.SaveChanges可能由于某种原因抛出错误.这些中的任何一个都会导致您在客户端看到的行为中显示的已记录消息.
您正在尝试使用您自己的自定义响应内容返回 400 响应(错误请求)。
我认为 IIS 默认不允许你这样做,正如 CodeThug 提到的,可能会用服务器消息替换你的自定义 JSON 内容。
但似乎您可以覆盖此行为:
http://develoq.net/2011/returning-a-body-content-with-400-http-status-code/
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5388 次 |
| 最近记录: |