简体   繁体   中英

when receiving a JSON http post response receiving error parsing it in ionic

I am creating a app in ionic framework and I am doing an http get to a backend REST API (it gets requested every time the button is click) that sends a response in JSON format.

button:

<div class="RequestButton">
<div class="button button-assertive" ng-click="request()">
  <a class="button">Request</a>
</div>
</div>

Here is the code from the app that makes the call to the backend:

$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

                        })

When the backend gives the response back to the app ionic gives me this error in the console of the webpage:

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

Not sure why I am getting this error and ionic won't let me do anything to the JSON response. I am not sure if I am doing this incorrectly

Could be an issue with values enclosed in single quotes. Checkout this post which has the answer to your question.

JSON.parse: expected property name or '}'

I am very sure that the response json which your server sends is not in correct format. I would suggest to first validate the server response using JSON Validator

And do the necessary changes at server side. I believe after this, your problem will be resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM