未捕获的语法错误:住而不是本地服务器上意外标记乙 [英] Uncaught SyntaxError: Unexpected token B on live but not local server

查看:192
本文介绍了未捕获的语法错误:住而不是本地服务器上意外标记乙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想提出一些Ajax后,它似乎很好地工作在本地主机,但是当我发布到在Amazon EC2服务器,我得到未捕获的SyntaxError:意外的标记B.这似乎指向JSON解析失败。完全一样的数据库,同一个浏览器,而同样的方法被调用。为什么会在本地而不是在服务器上运行。

So i am making some ajax post and it seems to work fine on the localhost, but when I publish it to ec2 server on amazon, I get Uncaught SyntaxError: Unexpected token B. Which seems to point to JSON parsing failure. Exact same database, same browser, and same methods being called. Why would it work on local and not on the server.

$.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');
                }
            });

下面是在服务器端的基本布局:

Here is the basic layout on the server side:

[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);
            }

        }

尝试语句,我做了几个查询的数据库,并张贴在 Authorize.Net(的 https://api.authorize.net/soap/v1/Service.asmx

Within the try statement, I do a couple of queries to the database and post some calculations on Authorize.Net (https://api.authorize.net/soap/v1/Service.asmx)

如果有与Authorize.net Web服务调用的任何错误,那么我会返回错误是这样的:

If there are any error with Authorize.net web service calls then I return errors like this:

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);
}

此错误,我记录:

    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& 

completedSynchronously)

completedSynchronously)

推荐答案

您有其他岗位在<一个href="http://stackoverflow.com/questions/16199361/cant-find-action-only-on-live-server-works-fine-in-local-server">can't发现只有活服务器操作,工作正常,在本地服务器,所以我猜这个职位具体涉及的JavaScript片段,而不是服务器端件。

You have another post at can't find action only on live server, works fine in local server, so I'm guessing that this post is specifically related to the javascript pieces, not the server-side pieces.

这听起来像什么不好的事情发生在服务器上,服务器发回的一些类型的错误,当试图处理该响应的错误处理程序(在JavaScript)死亡。

It sounds like something bad happens on the server, the server sends back some type of error, and the your error handler (in javascript) dies when trying to handle that response.

我得到未捕获的SyntaxError:意外的标记B.这似乎指向   以JSON解析失败。

I get Uncaught SyntaxError: Unexpected token B. Which seems to point to JSON parsing failure.

这听起来很合理。让我们来看看code:

That sounds quite reasonable. Let's look at the code:

.ajax({
  ...
  error: function (xhr, textStatus, errorThrown) {
      var errorData = $.parseJSON(xhr.responseText);
      var errorMessages = [];
      ...
  },
  ...
});

我会强烈建议采取看看什么 xhr.responseText 是。我的猜测是,它不包含有效的JSON,所以parseJSON方法抛出了意外标记B'的错误。

I would highly recommend taking a look at what xhr.responseText is. My guess it that it does not contain valid JSON, so the parseJSON method throws the 'Unexpected token B' error.

要看看这个值,你可以把的console.log(xhr.responseText); 或者你可以在你的Web浏览器或小提琴手使用类似的JavaScript调试器的工具看看还有什么。

To look at this value, you could put console.log(xhr.responseText); or you could use a tool like the javascript debugger in your web browser or fiddler to see what is there.

我的猜测是,服务器发回一串的东西,如有像你这样的期待是服务器而不是JSON上的错误。我看到你有错误处理内置的 - 我的猜测是,可能是在你的错误处理错误,并没有什么要抓住它。我建议做服务器端的调试,看看是否有错误的地方,你是不是期待的。

My guess is that the server is sending back a string with something like There was an error on the server instead of JSON like you are expecting. I see that you have error handling built in - my guess is that there is an error within your error handling, and there is nothing to catch it. I would recommend doing debugging on the server side to see if there is an error somewhere that you are not expecting.

也许profile.messages的东西,只能用一次枚举,并且当您尝试再次做到这一点,它抛出一个错误。或者,也许DB.SaveChanges抛出一个错误的某些原因。无论是这些将导致所记录的消息,你与你的客户端看到的行为参见

Perhaps profile.messages is something that can only be enumerated once, and when you try to do it again it throws an error. Or maybe DB.SaveChanges is throwing an error for some reason. Either of these would result in the logged message that you see with the behavior you see on the client side.

这篇关于未捕获的语法错误:住而不是本地服务器上意外标记乙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆