json 未捕获的语法错误:意外的令牌 < 在 Chrome 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19573127/
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-09-03 19:57:46  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token < On Chrome

jsongoogle-chrome

提问by Alen

I know this question has been asked many times but I can't find similarity with my issue. I'm getting this error only in Chrome, in every other browser everything is ok. I return data with JSON in several places but since my code works in other browsers I assume nothing is wrong with it. Chrome doesn't show me where is error (in my code) it shows me these errors:

我知道这个问题已经被问过很多次了,但我找不到与我的问题相似的地方。我只在 Chrome 中收到此错误,在所有其他浏览器中一切正常。我在几个地方用 JSON 返回数据,但由于我的代码在其他浏览器中工作,我认为它没有问题。Chrome 没有向我显示错误在哪里(在我的代码中)它向我显示了这些错误:

enter image description here

enter image description here

This is how I use JSON:

这就是我使用 JSON 的方式:

$.post("getData.php", {'id' : id}, function(data){
        var obj = jQuery.parseJSON(data);
.
.
.

... some mysqli query
$data = $query->fetch_assoc();
echo json_encode($data);

So I don't see a problem here, can someone help me with this.

所以我在这里没有看到问题,有人可以帮我解决这个问题。

采纳答案by Alen

Error with Uncaught SyntaxError: Unexpected token <using @Mario answer but that was only part of my problem. Another problem is, javascript doesn't get any data from PHP file. That was solved using this code, inside PHP file: header("Content-Type: text/javascript; charset=utf-8");This answer is found on this link, where I opened another question to solve this issue: Can't receive json data from PHP in Chrome and Opera

Uncaught SyntaxError: Unexpected token <使用@Mario 答案时出错,但这只是我问题的一部分。另一个问题是,javascript 无法从 PHP 文件中获取任何数据。这是在 PHP 文件中使用此代码解决的:header("Content-Type: text/javascript; charset=utf-8");此答案可在此链接上找到,我在其中打开了另一个问题来解决此问题:Can't receive json data from PHP in Chrome and Opera

回答by delueg

You can check your Network (console) and See the answer from the Server ... The "<" will be the first letter of your response. Something like "<"undefined index XY in line Z>"

您可以检查您的网络(控制台)并查看来自服务器的答案...“<”将是您回复的第一个字母。类似于“<”行 Z 中未定义的索引 XY>”

回答by luckyape

My solution to this is pretty unbelievable.

我对此的解决方案非常令人难以置信。

<script type="text/javascript" src="/js/dataLayer.js?v=1"></script>

The filename in the src attribute needed to be lowercase:

src 属性中的文件名需要小写:

<script type="text/javascript" src="/js/datalayer.js?v=1"></script>

and that somewhat inexplicably fixed the problem.

这有点莫名其妙地解决了这个问题。

In both cases the reference was returning 404 for testing.

在这两种情况下,引用都返回 404 进行测试。

回答by Must Nal

I got the same error ("Uncaught SyntaxError: Unexpected token <" ) at these two lines when testing a sample application .

在测试示例应用程序时,我在这两行中遇到了相同的错误(“Uncaught SyntaxError: Unexpected token <”)。

<script type = "text/javascript"  src="raphael-min.js"></script>
<script type = "text/javascript"  src="kuma-gauge.jquery.js"></script>

After a control, I realized that, local file locations are not correct and my local server app returns default page as the result. Client app can find the files but the founded files are default page, not the *.js files. So I receive Uncaught SyntaxError: Unexpected token <

控制后,我意识到,本地文件位置不正确,结果我的本地服务器应用程序返回默认页面。客户端应用程序可以找到文件,但创建的文件是默认页面,而不是 *.js 文件。所以我收到 Uncaught SyntaxError: Unexpected token <

I changed to orginal location on the intenet andit solved.

我更改为互联网上的原始位置并解决了。

<script type = "text/javascript"  src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js"></script>
<script type = "text/javascript"  src="//www.jqueryscript.net/demo/Creating-Animated-Gauges-Using-jQuery-Raphael-js-kumaGauge/js/kuma-gauge.jquery.js"></script>

回答by McRui

To override the error that you might experience in Chrome (and probably in Safari), try to set the Ajax parameter as dataType: "json". Then you shouldn't call parseJSON()on the objbecause the response you'll get comes deserialized.

要覆盖您在 Chrome(也可能在 Safari)中遇到的错误,请尝试将 Ajax 参数设置为dataType: "json". 那么你不应该调用parseJSON()obj因为你得到的响应是反序列化的。

回答by FreeFend

header("Location: route_to_main_page");

I've encountered this problem while having a custom 404 error handling file. Instead of throwing some html content it was suppose to redirect to the main page url. It worked well.

我在拥有自定义 404 错误处理文件时遇到了这个问题。它不是抛出一些 html 内容,而是应该重定向到主页 url。它运作良好。

Then I used this as a skeleton for a next project, therefore not changing the "route_to_main_page" actually on the new project (different URL) became a "route_to_external_url" so any 404 errors within your code (missing css stylesheets, js libraries) would return the "Uncaught SyntaxError: Unexpected token <".

然后我将其用作下一个项目的骨架,因此实际上没有更改新项目(不同的 URL)上的“route_to_main_page”变成了“route_to_external_url”,因此代码中的任何 404 错误(缺少 css 样式表、js 库)都会返回“未捕获的语法错误:意外标记<”。

回答by Tirth Patel

I have commented my this code : // $('#description').val('<?php echo $_POST['description']; ?>');and I got that error.

我已经评论了我的这段代码:// $('#description').val('<?php echo $_POST['description']; ?>');我得到了那个错误。

回答by E2rdo

It could be that the resource you're trying to request is under restricted access via your web app configuration, i.e. user must be logged for the application to serve the file.

可能是您尝试请求的资源通过您的 Web 应用程序配置受到限制访问,即用户必须登录,应用程序才能提供文件。

Try adding this to your Web.Config file (this is for .NET applications):

尝试将其添加到您的 Web.Config 文件(这是用于 .NET 应用程序):

<location path="js/resourcefile.js">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

You can place it anywhere before the closing configurationtag.

您可以将其放置在关闭配置标记之前的任何位置。

回答by Jens Meinecke

change it to

将其更改为

$.post({ 
         url = 'getData.php',  
         data : { 'id' id } , 
         dataType : 'text'
       });

This way ajax will not try to parse the data into json or similar

这样 ajax 就不会尝试将数据解析为 json 或类似的

回答by anoraq

Seems everyone has difference experiences from this and therfore solutions as well :) This is my "story".

似乎每个人对此都有不同的体验,因此也有不同的解决方案:) 这是我的“故事”。

My thing came from a validate.php file fetched with ajax. The output was meant to be :

我的东西来自使用 ajax 获取的 validate.php 文件。输出是为了:

$response['status'] = $status;
$response['message'] = $message;
$response['param'] = $param;

echo json_encode($response);

And the error that cause the "Unexpected token <" error was simply that in some cases $message hadn't been declared (but only $status and $param). So, added this in the beginning of the code.

导致“意外令牌<”错误的错误只是在某些情况下没有声明 $message(但只有 $status 和 $param)。所以,在代码的开头添加了这个。

$message = ''; // Default value, in case it doesn't get set later on.

So I guess, those "little things" may in this scenario big of quite importance. So be sure to really check your code and making it bulletproof.

所以我想,在这种情况下,那些“小事”可能非常重要。所以一定要真正检查你的代码并使其防弹。