$http.get returns Html code in response instead of json object

点点圈 提交于 2019-12-24 11:53:55

问题


I'm trying to get some data from an external API using angular $http.get method , but i'm getting 'html code' instead of 'json object' in response. When i tried calling the API using jquery ajax it worked fine. Here's the code

  $http.get({
   "url": 'https://ebaydemo.stamplayapp.com/api/cobject/v1/projects',
   "crossDomain": true,
   "headers": {
      "accept": "application/json",
      "content-type": "application/json"
    },
    "params": {
      "populate": false,
      "n": 10
    },
    "processData": false
  }).success(function (response) {
   console.log(response);// Response has 'html code'
  });

Edit Here's the short snippet of response i'm getting now:

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--   >
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<base href="/">
<title></title>

回答1:


I created a little HTML snippet to call your code like below:

<html>
<body ng-app="app" ng-controller="eBayController">
<input type="button" value="EBay" ng-click="getFromEbay()"></input>
<script rc="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="ebay.js"></script>
</body>

and then just reduced the JavaScript to this:

/* global angular */
angular.module("app",[])
.controller('eBayController', function($scope,$http) {
    $scope.getFromEbay = function() {
        $http.get("https://ebaydemo.stamplayapp.com/api/cobject/v1/projects")
        .success(function (response) 
        {
            console.log(response);
        }); 
   }

})

and I'm getting a JSON object back.




回答2:


Your request is in a wrong format. Here is a plunker where you can see how it works.

http://plnkr.co/edit/QFCs8kiPvcErmeRePBMd

Take a look at app.js and how the $http is being used



来源:https://stackoverflow.com/questions/32969774/http-get-returns-html-code-in-response-instead-of-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!