AngularJS 没有得到 API 响应
AngularJS not getting API Response
在 OSGi 环境 (KARAF) 中使用 AngularJS 和 CXF-JAXRS 库 运行,我试图让一个简单的 API 响应正常工作。
日志显示 AngularJS 已正确连接到 REST 服务,并且已正确响应 200 状态代码。但是,在 AngularJS 中,没有检索到发回的信息。
仅供参考:我不得不从所有链接中删除 "http://",因为 StackExchange 告诉我我没有足够的声誉来包含它们。但它们确实存在 code/logs.
我正在连接的 REST 方法
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(){
String testResponse = "Success.";
Response responseString = Response
.status(200)
.entity(testResponse)
.build();
return responseString;
}
AngularJS 控制器
app.controller('HttpController', function($scope, $http){
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test'
}).then(function successCallback(response){
$scope.testString = response.data;
}, function errorCallback(response){
$scope.testString = response;
});
$scope.validationString = "Controller is functional.";
});
AngularJS Display Code
<div ng-controller="HttpController">
The response is {{ testString }}<br>
{{validationString}}
显示在 AngularJS HTML 页
连接时显示KARAF日志
2017-01-10 11:42:30,133 |信息 | qtp84694963-897 |登录拦截器 | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT |入站消息
编号:20
地址:localhost:8181/cxf/strsoftware/test
Http-Method:得到
Content-Type:
Headers: {Accept=[application/json, text/plain, /], accept-encoding=[gzip, deflate , sdch, br], Accept-Language=[en-US,en;q=0.8], 连接=[keep-alive], Content-Type=[null], 主机=[localhost:8181], 来源=[localhost:63343], Referer=[localhost:63343/STRFrontEnd/StartScreen.html?_ijt=qsu1c8a1qskj0def9ho1rml4hv], User-Agent=[Mozilla/5 .0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, 像 Gecko) Chrome/55.0.2883.87 Safari/537.36]}
2017-01-10 11:42:30,135 |信息 | qtp84694963-897 |注销拦截器 | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT |出站消息
编号:20
Response-Code: 200
Content-Type: application/json
Headers:{Content-Type=[application/json],日期=[2017 年 1 月 10 日星期二 16:42:30 GMT]}
有效负载:成功。
更新
通过在 Chrome (Ctrl+Shift+I) 上打开检查页面并单击 'Console',我们发现了以下错误:
angular.js:11756XMLHttpRequest 无法加载 localhost:8181/cxf/strsoftware/test。仅协议方案支持跨源请求:http、data、chrome、chrome-extension、https、chrome-extension-resource.
这是一个 Server-Side 错误,因此我们目前正在 Server-Side 中实施 CORS 以查看是否可以解决问题。找到后会post解决。
不确定你的js代码是否正确。我也尝试过 angular js,我的代码如下所示:
$http.get("/cxf/strsoftware/test")
.success(function (response) {$scope.testString = response.data;});
请参阅 blueprint-cdi-example 查看我的完整代码。
您正在 .then() 中调用匿名函数。
所以应该是这样的:
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test'
}).then(function(response){
$scope.testString = response.data;
}, function(response){
$scope.testString = response;
});
尝试这个,如果不起作用 将数据:'' 替换为 params:{} :
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test',
// Just to be sure! maybe you don't need it
dataType: 'json',
headers: {
"Content-Type": "application/json"
},
//To be sure Angular does NOT remove the content-type header.
data: '',
}).then(function successCallback(response) {
$scope.testString = response.data;
}, function errorCallback(response) {
$scope.testString = response;
});
编辑
查看此答案下方的评论,了解 Shadmehr 在何处解决了问题。解决方案是在应用程序的服务器端部分实施 CORS 过滤器。尝试访问 API.
时,浏览器出现跨区域错误
在 OSGi 环境 (KARAF) 中使用 AngularJS 和 CXF-JAXRS 库 运行,我试图让一个简单的 API 响应正常工作。
日志显示 AngularJS 已正确连接到 REST 服务,并且已正确响应 200 状态代码。但是,在 AngularJS 中,没有检索到发回的信息。
仅供参考:我不得不从所有链接中删除 "http://",因为 StackExchange 告诉我我没有足够的声誉来包含它们。但它们确实存在 code/logs.
我正在连接的 REST 方法
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(){
String testResponse = "Success.";
Response responseString = Response
.status(200)
.entity(testResponse)
.build();
return responseString;
}
AngularJS 控制器
app.controller('HttpController', function($scope, $http){
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test'
}).then(function successCallback(response){
$scope.testString = response.data;
}, function errorCallback(response){
$scope.testString = response;
});
$scope.validationString = "Controller is functional.";
});
AngularJS Display Code
<div ng-controller="HttpController">
The response is {{ testString }}<br>
{{validationString}}
显示在 AngularJS HTML 页
连接时显示KARAF日志
2017-01-10 11:42:30,133 |信息 | qtp84694963-897 |登录拦截器 | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT |入站消息
编号:20 地址:localhost:8181/cxf/strsoftware/test Http-Method:得到 Content-Type:
Headers: {Accept=[application/json, text/plain, /], accept-encoding=[gzip, deflate , sdch, br], Accept-Language=[en-US,en;q=0.8], 连接=[keep-alive], Content-Type=[null], 主机=[localhost:8181], 来源=[localhost:63343], Referer=[localhost:63343/STRFrontEnd/StartScreen.html?_ijt=qsu1c8a1qskj0def9ho1rml4hv], User-Agent=[Mozilla/5 .0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, 像 Gecko) Chrome/55.0.2883.87 Safari/537.36]}
2017-01-10 11:42:30,135 |信息 | qtp84694963-897 |注销拦截器 | 107 - org.apache.cxf.cxf-core - 3.2.0.SNAPSHOT |出站消息
编号:20 Response-Code: 200 Content-Type: application/json Headers:{Content-Type=[application/json],日期=[2017 年 1 月 10 日星期二 16:42:30 GMT]}
有效负载:成功。
更新
通过在 Chrome (Ctrl+Shift+I) 上打开检查页面并单击 'Console',我们发现了以下错误:
angular.js:11756XMLHttpRequest 无法加载 localhost:8181/cxf/strsoftware/test。仅协议方案支持跨源请求:http、data、chrome、chrome-extension、https、chrome-extension-resource.
这是一个 Server-Side 错误,因此我们目前正在 Server-Side 中实施 CORS 以查看是否可以解决问题。找到后会post解决。
不确定你的js代码是否正确。我也尝试过 angular js,我的代码如下所示:
$http.get("/cxf/strsoftware/test")
.success(function (response) {$scope.testString = response.data;});
请参阅 blueprint-cdi-example 查看我的完整代码。
您正在 .then() 中调用匿名函数。 所以应该是这样的:
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test'
}).then(function(response){
$scope.testString = response.data;
}, function(response){
$scope.testString = response;
});
尝试这个,如果不起作用 将数据:'' 替换为 params:{} :
$http({
method: 'GET',
url: 'localhost:8181/cxf/strsoftware/test',
// Just to be sure! maybe you don't need it
dataType: 'json',
headers: {
"Content-Type": "application/json"
},
//To be sure Angular does NOT remove the content-type header.
data: '',
}).then(function successCallback(response) {
$scope.testString = response.data;
}, function errorCallback(response) {
$scope.testString = response;
});
编辑
查看此答案下方的评论,了解 Shadmehr 在何处解决了问题。解决方案是在应用程序的服务器端部分实施 CORS 过滤器。尝试访问 API.
时,浏览器出现跨区域错误