[英]Using Restangular with the same URL but different files
我需要通過Angular與REST Web服務進行通信,並且正在使用RestAngular,但遇到了一些困難。 我必須使用相同的URL,但要對兩個不同的文件進行GET&POST。 我在app.js的.config中設置基本URL,而service.js是我通過RestangularConfigurer設置其他URL的地方。 配置中的BaseURL附加了查詢字符串。 服務中的第二個URL沒有。 我想從配置中的BaseURL中獲取,然后從輸入中POST到工廠。 以下是我的代碼示例
'use strict';
angular
.module('testApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'ui.bootstrap',
'ngGrid',
'google-maps',
'ngMessages',
'restangular'
])
.config(function (RestangularProvider, $routeProvider) {
RestangularProvider.setBaseUrl('http://dev.domain.com:9005/question-ws.htm?&areacode=215');
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
});
'use strict';
angular.module('testApp')
.factory('NextRestangular', function($http, $sce, Restangular){
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://dev.domain.com:9005/next-question-ws.htm');
});
});
'use strict';
angular.module('testApp')
.controller('ScreenCtrl', function ($scope, Restangular, NextRestangular) {
Restangular.all().getList();
NextRestangular.all().getList();
});
我似乎無法從Restangular獲取,也無法發布到NextRestangular。 文件結構可以支持嗎? 我要解決所有這些錯誤嗎?
Restangular是用於處理Rest API的Restful資源的服務。
您網址中的.htm導致我認為響應不是JSON。 我不相信Restangular在這種情況下會為您工作。 我建議調查ngResource甚至$ http。 如果您的響應是JSON,以下內容將幫助您上路。
在此示例中,baseUrl應該為“ http://dev.domain.com:9005 ”。
RestangularProvider.setBaseUrl('http://dev.domain.com:9005');
我以前沒有使用過setRequestSuffix ,但是這可能也使您擁有.htm。
RestangularProvider.setRequestSuffix('.htm');
然后,您的API調用應如下所示。
Restangular.all('next-question-ws').get({areacode: 215});
Restangular.all('next-question-ws').post(
// JSON Payload here.
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.