ana*_*312 19 javascript ajax jquery angularjs
我是一名经验丰富的Flex开发人员,正在学习AngularJS.这太令人困惑了!
无论如何,我试图通过SOAP WSDL请求对我的后端(在同一域)服务器进行服务调用,并使用AngularJS模型对象填充数据.我正在尝试使用Ajax但是在获取实际数据方面存在一些问题.我认为创建SOAP标记的方式有问题.我收到了成功的回复,但没有数据.
After not being able to figure out the Ajax method, I came across soapclient.js and found it to be extremely easy, with less code than Ajax. soapclient.js does most of the work for you, similar to the Ajax method, which makes much less code. Additionally, using soapclient.js I am able to make the SOAP call and also get data back the XML formatted response.
http://javascriptsoapclient.codeplex.com
My issue is that I am trying to use AngularJS to dump the XML response into an AnularJS model object. I am not sure how to setup the AngularJS project for what I am doing, but I would really like to know the best method in order to keep what I am working on decoupled. I have been searching Google like crazy, but most examples seem overly complicated for a beginner.
Here is what I have:
<html>
<head>
<script language="JavaScript" type="text/javascript" src="jquery-1.10.1.js"></script>
<script language="JavaScript" type="text/javascript" src="soapclient.js"></script>
<script type="text/javascript">
function getData() {
var url2 = "https://myService";
var pl = new SOAPClientParameters();
pl.add("arg0", false);
SOAPClient.invoke(url2, "methodToCall", pl, true, getDataCallback);
}
function getDataCallback(r, soapResponse) {
alert(r.contents.payeeMailName);
}
</script>
</head>
<body>
<form>
<input type="button" value="Click Here to Call Web Service" onClick="getData()" style="width: 192px">
</form>
<div id="result">Result?</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Now, the SOAP service returns the data like this:
<return>
<contents>
<eftAcctType>S</eftAcctType>
<id>
<djNumber>201-16-39063</djNumber>
<djSequence>1</djSequence>
</id>
<payeeAddrLine1>124 Agate Drive</payeeAddrLine1>
</contents>
<contents>
<eftAcctType/>
<id>
<djNumber>201-16-39201</djNumber>
<djSequence>1</djSequence>
</id>
<payeeAddrLine1>c/o Kevin Martinez, Attorney at Law</payeeAddrLine1>
</contents>
<contents>
<eftAcctType>C</eftAcctType>
<id>
<djNumber>201-16-38561</djNumber>
<djSequence>1</djSequence>
</id>
<payeeAddrLine1>1360 South Highway 191</payeeAddrLine1>
</contents>
<status>0</status>
</return>
Run Code Online (Sandbox Code Playgroud)
在AngularJS中进行服务调用的"正确"方式是什么,假设我这样做的方式没问题,如果不是让我知道最好的方法,那么在结果中,我如何在XML响应中循环数据并将其解析为Angular模型对象?我最终想在DataGrid中使用这些数据.
任何帮助都感激不尽.
And*_*ery 19
两年后,我已经构建了一个专门用于处理GitHub上的SOAP Web服务的Angular模块.
https://github.com/andrewmcgivery/angular-soap
这是一篇关于如何使用它的博客文章:http: //mcgivery.com/soap-web-services-angular-ionic/
长话短说,它允许你做这样的事情:
angular.module('myApp', ['angularSoap'])
.factory("testService", ['$soap',function($soap){
var base_url = "http://www.cooldomain.com/SoapTest/webservicedemo.asmx";
return {
HelloWorld: function(){
return $soap.post(base_url,"HelloWorld");
}
}
}])
.controller('MainCtrl', function($scope, testService) {
testService.HelloWorld().then(function(response){
$scope.response = response;
});
})
Run Code Online (Sandbox Code Playgroud)
小智 8
我想最好的方法是将它实现为$ http拦截器.我在我们的项目中做到了它并且工作得很好,因为角度$ http调用保持不变.
这是我为项目创建的提供程序的链接:http://jsfiddle.net/gqp9m/
我从soapclient库中进行了一些复制粘贴,并将其移动到提供程序中.我也改变了一些语法,所以代码将通过jsHint.大多数修改过的函数都附有文档说明.它还需要jQuery(对于$ .parseXML函数 - 你可以重构它来删除jQuery中的依赖).
最大的区别是我的代码没有在第一次请求时加载wsdl,而是需要你在进行任何调用之前对其进行缓存,如下所示:
myModule.service(['myModule.soap-interceptor', function(soap){
$http.get('http://www.myveryfakedomain.com/CRMAPIWS74?wsdl',
{ isJSON: true }).then(function(result){
soap.setWSDL('http:/www.myveryfakedomain.com/CRMAPIWS74', result.data);
});
}]);
Run Code Online (Sandbox Code Playgroud)
soap是注入的soap-interceptor实例.你调用wsdl然后调用soap.setWSDL传递它的基本URL和解析的wsdl.另请注意传递给$ http调用的isJSON参数.这是因为默认情况下我的代码将每个调用都视为SOAP请求.这就是拦截器的作用.isJSON:true将允许你使用$ http作为上帝的意图;)
在调用setWSDL后,只需调用$ http,就像你总是这样:
$http.get('http:/www.myveryfakedomain.com/CRMAPIWS74/action').then(function(result){
// do something...
});
Run Code Online (Sandbox Code Playgroud)
请记住,此代码是为我们的项目编写的,它不是受支持的开源项目或其他东西.在使用之前可能需要一定程度的维护或重构,但这是一个良好的开端.
| 归档时间: |
|
| 查看次数: |
57498 次 |
| 最近记录: |