Javascript 语法错误:JSON 中的意外标记 e 位于位置 1

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

SyntaxError: Unexpected token e in JSON at position 1

javascriptangularjsajax

提问by sidgate

What's wrong with below code? I keep getting following error. The response from ajax is plain text, for instance "Hello world". Does $http.get() expects json response?

下面的代码有什么问题?我不断收到以下错误。来自 ajax 的响应是纯文本,例如“Hello world”。$http.get() 是否需要 json 响应?

angular.js:13550 SyntaxError: Unexpected token t in JSON at position 1 at Object.parse (native) at uc (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:17:36) at ac (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:91:229)

angular.js:13550 SyntaxError: Unexpected token t in JSON at position 1 at Object.parse (native) at uc ( https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js :17:36) 在 ac ( https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:91:229)

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">


<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0-rc2/angular-material.min.css">


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0-rc2/angular-material.min.js"></script>




<script>
    var resourceApp = angular.module("resourceApp", [ "ngMaterial" ])
            .controller('resourceController', function resourceController($scope, $http) {
                $scope.processForm = function() {
                    $http.get("test")
                        .then(function(response) {
                            console.log(response.data);

                        });
                };
            });
</script>
</head>
<body ng-app="resourceApp" ng-controller="resourceController">

    <button ng-click="processForm()">Submit</button>
    <pre>{{response}}</pre>
</body>
</html>

采纳答案by Yassine Badache

For your second questionNot necessarily. You should check both in your server and your front-end configuration that you are actually allowed to receive plain text as a response.

对于你的第二个问题不一定。您应该在服务器和前端配置中检查您实际上允许接收纯文本作为响应。

The official $http documentationwill help you about setting your header correctly, while I let you check for your server configuration as well.

官方 $http 文档将帮助您正确设置标头,同时我也让您检查服务器配置。

For the first one

对于第一个

Is "test" a valid route ? It seems that $http tries to get "test" as a plain text, and not the resource which is stored behind "test", as the firt incorrect character is 't'. Otherwise (if it is a valid route), please refer on the above-mentioned answer.

“测试”是一条有效的路线吗?似乎 $http 试图将“test”作为纯文本而不是存储在“test”后面的资源,因为第一个不正确的字符是“t”。否则(如果是有效路线),请参考上述答案。

EDIT

编辑

When calling $http (even in localhost), you must mention the full route to your resource. Here, you cannot directly call ("test"), it interprets it as a plain "test" request. Try calling:

调用 $http 时(甚至在localhost 中),您必须提及到您的资源的完整路由。在这里,您不能直接调用 ("test"),它会将其解释为一个简单的“测试”请求。尝试调用:

$http.get('localhost:[your port]/test').then { ...what you need to do... });

$http.get('localhost:[your port]/test').then { ...what you need to do... });

SECOND EDIT

第二次编辑

text/plainand plain/textmakes a difference when configuringn, be careful ! This fixed the asker's problem.

text/plainplain/text在配置时有所作为,小心!这解决了提问者的问题。