[英]when receiving a JSON http post response receiving error parsing it in ionic
我正在離子框架中創建一個應用程序,並且正在通過HTTP獲取后端REST API(每次單擊該按鈕都會被請求),該請求以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格式不正確。 我建議首先使用JSON Validator驗證服務器響應
並在服務器端進行必要的更改。 相信此后,您的問題將得到解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.