Javascript 意外令牌 < JSON 中的位置 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36814694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:35:18  来源:igfitidea点击:

Unexpected token < in JSON at position 4

javascriptjqueryjsonajax

提问by Himanshu Singh

var url="http://fsa.citop.in/lnct/service/signProcess.aspx";
var data={txtLogId: "[email protected]",txtLogPass: "xyz",hdnReqType2: "sign87162"};
var success=function(data, textStatus, jqXHR) {
            console.log(data);
         };

var fail=function(jqXHR, textStatus, errorThrown) {
         console.log("Error:" + errorThrown );
     }
$.ajax({
  type: "POST",
  url: url,
  data:data,
  success:success,
  error:fail,

});

This POST request gives me the error, SyntaxError: Unexpected token < in JSON at position 4, in the console of the page 'http://fsa.citop.in/lnct/' in chrome.

此 POST 请求在 chromeSyntaxError: Unexpected token < in JSON at position 4页面“ http://fsa.citop.in/lnct/”的控制台中给了我错误。

But if I use fsa.citop.in/lnct/service/signProcess.aspx(i.e. no http://), it gives me no error, but nothing comes back in data. On successof POST request, a JSON object is expected. Please somebody explain what is happening here and how it could be resolved.

但是如果我使用fsa.citop.in/lnct/service/signProcess.aspx(即没有 http://),它不会给我任何错误,但数据中没有任何返回。在successPOST 请求中,需要一个 JSON 对象。请有人解释这里发生了什么以及如何解决。

采纳答案by Gabriel

It's most likely because the response is HTML and it's trying to parse it as something else. The <at position 4 is the first < of <!DOCTYPE html....

这很可能是因为响应是 HTML 并且它试图将其解析为其他内容。该<4位是第一<的<!DOCTYPE html...

You should try to specify dataType in your ajax call (see http://api.jquery.com/jquery.ajax/) and also make signProcess.aspxto return something more useful (currently the response content type is application/jsonbut it prints HTML).

您应该尝试在 ajax 调用中指定 dataType(请参阅http://api.jquery.com/jquery.ajax/)并signProcess.aspx返回更有用的内容(当前响应内容类型是application/json但它打印 HTML)。

回答by David Salamon

For those who encounter this problem in AWS Lambda code editor it is most likely your session has timed out.

对于在 AWS Lambda 代码编辑器中遇到此问题的人,很可能您的会话已超时。

Try reloading the page and signing in again. It should resolve this.

尝试重新加载页面并再次登录。它应该解决这个问题。