【发布时间】:2016-07-24 16:41:00
【问题描述】:
我正在 ionic 框架中创建一个应用程序,并且我正在对后端 REST API 进行 http 获取(每次单击按钮时都会请求它),它以 JSON 格式发送响应。
按钮:
<div class="RequestButton">
<div class="button button-assertive" ng-click="request()">
<a class="button">Request</a>
</div>
</div>
以下是调用后端的应用程序代码:
$scope.request = function () {
var requestURL = API_URL + "request";
$http.post(requestURL, "userID=TJones92&cat=Mobile", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(
function (res) {
$scope.response = res.data
})
当后端将响应返回给应用程序时,离子在网页的控制台中给我这个错误:
Error: JSON.parse: expected property name or '}' at line 1 column 5 of the JSON data
fromJson@http://localhost:8100/lib/ionic/js/ionic.bundle.js:14543:9
defaultHttpResponseTransform@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22560:16
transformData/<@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22651:12
forEach@http://localhost:8100/lib/ionic/js/ionic.bundle.js:13648:11
transformData@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22650:3
transformResponse@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23406:23
processQueue@http://localhost:8100/lib/ionic/js/ionic.bundle.js:27879:28
scheduleProcessQueue/<@http://localhost:8100/lib/ionic/js/ionic.bundle.js:27895:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29158:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:28969:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29263:13
done@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23676:36
completeRequest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23848:7
requestLoaded@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23789:9
不知道为什么我会收到这个错误,而且 ionic 不会让我对 JSON 响应做任何事情。我不确定我这样做是否不正确
【问题讨论】:
-
看起来服务器的响应不是真正的 json。您也可以发表您的回复吗?
-
检查浏览器开发工具网络中的实际请求以查看实际返回的内容。显示你的 php 输出代码
-
我认为您的服务器发送的 json 格式错误导致错误。
-
添加您收到的 JSON 响应。
标签: javascript angularjs json ionic-framework