nodejs restful api返回错误状态-1 at ionic angularjs $ http

时间:2016-06-11 20:09:48

标签: javascript angularjs node.js api http

我正在制作get请求的离子angularjs代码如下所示:

.factory('Tasks', function($http) {
  return {
    all: function() {
      $http({
  method: 'GET',
  url: 'http://localhost:3000/hello'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    alert(JSON.stringify(response))
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    alert(JSON.stringify(response))
  });

      $http({
  method: 'GET',
  url: 'http://localhost:3000/hello'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    alert(JSON.stringify(response))
  });
 }
}

以下哪些提醒:

{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://localhost:3000/hello","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

我的nodejs api服务器如下所示:

app.get('/hello', function(req,res){
    res.json({hello: "hi"})

})

我的卷毛:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET localhost:3000/hello

返回:

HTTP/1.1 200 OK
X-Powered-By: Express
content-type: application/json
content-length: 14
etag: "-1509679108"
Date: Sat, 11 Jun 2016 20:06:29 GMT
Connection: keep-alive

{"hello":"hi"}

令人惊讶的是,它可以正常使用此测试网址: http://jsonplaceholder.typicode.com/posts/1

我需要做些什么才能让它发挥作用?

0 个答案:

没有答案