【问题标题】:Unable to call my restful service from angularjs app using $http无法使用 $http 从 angularjs 应用程序调用我的宁静服务
【发布时间】:2014-03-09 02:42:50
【问题描述】:


我无法使用 angularjs $http 从 rest webservice 读取 JSON。我在一个返回 JSON 的不同项目中有一个简单的休息 web 服务。当我尝试从角度访问休息服务时,它会进入错误部分。

以下是我的代码: Java 中的 Restful 服务:

package com.demoapp.rest;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 */
@Path("Employee")
public class EmployeeResource {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of EmployeeResource
     */
    public EmployeeResource() {
    }

    /**
     * Retrieves representation of an instance of com.demoapp.rest.EmployeeResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public String getJson() {
        //TODO return proper representation object
        return "({\"id\":3,\"name\":\"Joe\"})";
    }

    /**
     * PUT method for updating or creating an instance of EmployeeResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }
}

Angularjs 代码:

angular.module('myApp.controllers', [])

 .controller('MyCtrl1', ['$scope', '$http', function($scope, $http) {

    $http.jsonp( 
 /*Doesn't work*/ 'http://localhost:8080/mavenproject1/webresources/Employee?callback=JSON_CALLBACK'
 /*Works*/ /*'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&callback=JSON_CALLBACK'*/
      )

        .success(function(data) {
            console.log('Success '+data);                
        })
        .error(function(data) {
            console.log('Error '+data);
        });

  }])
  .controller('MyCtrl2', [function() {

  }]);

我的第一个 url (localhost..) 出现错误,并且相同的 angularjs 代码适用于另一个公共的 restful 服务。
谁能告诉我为什么 angularjs 在控制台中为 (http://localhost..) RESTful 服务返回错误并为 (http://api.openweathermap.org/....) 成功?
我到底哪里错了?

【问题讨论】:

标签: json web-services angularjs rest


【解决方案1】:

您正试图通过jsonp 访问资源,但您的REST 服务返回纯json。你需要使用

$http.get('http://localhost:8080/mavenproject1/webresources/Employee')

网址http://api.openweathermap.org 有效,因为它们返回jsonp 格式。如果您向其他域发出请求,则需要此格式。 Jsonp 表示将结果包装在函数调用中。该函数的名称是动态生成的,并由您的示例中的回调参数指定。

编辑(上述讨论的结果 - 在生产中,此应用程序还将在两个不同的服务器上运行,因此有以下选项可以解决问题)

1) 您可以在您的 jboss 服务器中使用 Access-Control-Allow-Origin 标头。看看这个答案如何做到这一点:Set response headers not using filter - RESTeasy

2) 你可以在你的 tomcat 和你的 jboss 服务器前面使用一个反向代理。例如 apache 网络服务器可以做到这一点。这是解决此问题的答案:How to correctly configure a reverse proxy with Apache, to be used for cross-domain AJAX?

3) 你可以切换到 jsonp - 这有点复杂,因为 RESTEasy 不直接支持它。看看这个帖子:How enable JSONP in RESTEasy?

【讨论】:

  • 我曾尝试使用 GET,但随后出现以下错误:XMLHttpRequest 无法加载 localhost:8080/mavenproject1/webresources/Employee。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'localhost:8383'。
  • 我的 REST Web 服务在 8080 端口上运行,而我的 AngularJS Web 应用程序在 8383 端口上运行。在 REST Web 服务中需要做什么才能返回 JSONP 以便它在 AngularJS 中返回成功?
  • localhost:8383 与 localhost:8080 的来源不同。我猜您的静态内容是由另一台服务器提供的,而不是您的后端运行。有不同的方法可以解决这个问题。但这取决于您的需求。您可以在服务端设置此标头,您可以使用代理,以便您的端口 8080 对浏览器可见为 8383。或者您切换到 jsonp。你是如何运行你的 Angular 应用程序的?来自 grunt serve?
  • 我的 webservice 应用程序已部署到 Jboss 服务器,现在,angularjs 应用程序使用 Chrome 中的 Netbeans 连接器运行。如果您能告诉我如何切换到 jsonp 会很有帮助?
  • 好的,您的应用程序将如何在生产中运行?也在两个不同的服务器上?
猜你喜欢
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 2011-12-10
相关资源
最近更新 更多