SyntaxError:JSON中位置1处的意外令牌e [英] SyntaxError: Unexpected token e in JSON at position 1

查看:65
本文介绍了SyntaxError:JSON中位置1处的意外令牌e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有什么问题?我一直在跟踪错误.来自ajax的响应是纯文本,例如"Hello world".$ http.get()是否期望json响应?

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?

angular.js:13550 SyntaxError:位置1的JSON中的意外令牌t在Object.parse(本机)在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>

推荐答案

第二个问题不必要.您应该在服务器和前端配置中都进行检查,以确认实际上允许您接收纯文本作为响应.

For your second question Not 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.

官方$ http文档将帮助您设置标题正确,而我也让您检查服务器配置.

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

第一个

是测试"有效的路线?似乎$ http试图获取"test"消息.因为不正确的字符是"t",因此是纯文本,而不是存储在"test"后面的资源.否则(如果这是一条有效的路线),请参考上述答案.

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时(甚至在 localhost 中),您必须提及到资源的完整路径.在此,您不能直接调用("test"),而是将其解释为普通的"test".要求.尝试致电:

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.get('localhost:[您的端口]/test').然后{...您需要做什么...});

第二编辑

text/plain plain/text 在配置时会有所不同,请注意!这解决了询问者的问题.

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

这篇关于SyntaxError:JSON中位置1处的意外令牌e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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