2014-02-11 27 views
0


我无法使用angularjs $ http从其他webservice中读取JSON。我在一个返回JSON的不同项目中有一个简单的休息webservice。当我试图从角度打休息服务时,它会转到错误部分。

下面是我的代码: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(本地主机。)和同样angularjs代码适用于另一个公共的安宁服务。
任何人都可以请告诉为什么angularjs为(http://localhost.。)宁静的服务在控制台中返回错误并且成功为(http://api.openweathermap.org/...。)?
我到底错在哪里?

+0

首先尝试在您的浏览器中的localhost url,它工作正常吗? –

+0

是的,本地主机url在浏览器中工作。 url - http:// localhost:8080/mavenproject1/webresources/Employee并返回 - ({“id”:3,“name”:“Joe”}) –

回答

1

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

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

url http://api.openweathermap.org的工作原理是因为它们返回jsonp格式。如果您向其他域请求,则需要此格式。 Jsonp意味着结果被封装在一个函数调用中。该函数的名称是动态生成的,并由您的示例中的回调参数指定。

EDIT(从上面的讨论结果 - 在生产这个应用程序也将在两个不同的服务器上运行,所以有下列选项来解决问题)

1)你可以使用访问控制允许-Origin标头在你的jboss服务器中。看看这个答案如何做到这一点:Set response headers not using filter - RESTeasy

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

3)您可以切换到JSONP - 这是一个有点复杂,因为它不是由直接的RESTEasy支持。看看这篇文章:How enable JSONP in RESTEasy?

+0

我曾尝试使用GET,但后来得到以下错误:XMLHttpRequest无法加载HTTP://本地主机:8080/mavenproject1/webresources /雇员。请求的资源上没有“Access-Control-Allow-Origin”标题。原因'http:// localhost:8383'因此不被允许访问。 –

+0

我的休息webservice运行在端口8080上,我的angularjs web应用程序运行在端口8383.在rest webservice中需要做什么才能返回JSONP,以便它在angularjs中返回成功? –

+0

localhost:8383与localhost:8080不一样。我猜你的静态内容由另一台服务器提供,而不是你的后端运行。有不同的方法可以解决这个问题。但这取决于你的需求。您可以在服务端设置此标头,您可以使用代理,以便您的端口8080对浏览器可见,如8383.或者切换到jsonp。你是如何运行你的角度应用程序?从咕噜服务? – michael

相关问题