[英]Javascript Cross Domain Web Service Call
I'm trying to call a .NET web service, from javascript, that returns an XML document. 我正在尝试从JavaScript调用.NET Web服务,该服务返回XML文档。 The web service is located on a different web server than my app.
Web服务与我的应用程序位于不同的Web服务器上。 I do not have control over the web service.
我无法控制Web服务。 I have successfully called the service using jQuery setting the dataType to jsonp and I can see that the XML document is returned in Firebug.
我已经成功使用jQuery将数据类型设置为jsonp调用了该服务,并且可以看到Firebug中返回了XML文档。 However, I get an error in the Firebug console "missing ; before statement" where it looks like it's trying to parse the returned xml (to json maybe??).
但是,我在Firebug控制台中遇到一个错误“ missing; before statement”,它似乎试图解析返回的xml(可能是json?)。 Any ideas or what's the best way to call a cross domain web service that returns xml?
有什么想法或调用返回xml的跨域Web服务的最佳方法是什么? Many Thanks!
非常感谢!
The JSON-P data format is a JavaScript program. JSON-P数据格式是JavaScript程序。 If you use JSON-P then the data you get back must be JavaScript.
如果使用JSON-P,则返回的数据必须是JavaScript。
JSON-P works by loading the document using a <script>
element (and that is never going to work for arbitrary XML data) JSON-P的工作方式是使用
<script>
元素加载文档(并且对任何XML数据永远都行不通)
Your options are: 您的选择是:
Someting like this: 像这样的东西:
$.getJSON("http://yoursite.com/xml2json.php?callback=?", {feed:"http://agency.nd.edu/agencynd-team.xml"}, function(data) { // process data here } }); <?php header('content-type: application/json; charset=utf-8'); if( strlen($_GET["feed"]) > 13 ) { $xml = file_get_contents(urldecode($_GET["feed"])); if($xml) { $data = @simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($data); echo isset($_GET["callback"]) ? "{$_GET[’callback’]}($json)" : $json; } } ?>
If you set dataType to jsonp you should get response wrapped in the function call. 如果将dataType设置为jsonp,则应将响应包装在函数调用中。 For example if you expect to get
例如,如果您希望得到
object {"key": "value"}
the response for jsonp should be jsonp的响应应该是
parseRespone({"key":"value"});
The reason for this is that jsonp adds <script> tag to your page header so it should append valid javascript code there. 原因是jsonp在页面标题中添加了<script>标记,因此它应该在其中附加有效的javascript代码。
If you have no possibility to contact with the developers of the server I would suggest to make ajax call to your page where the page would make curl connection to the .NET server and retrives the response in pure json. 如果您无法与服务器的开发人员联系,我建议对页面进行ajax调用,该页面将卷曲连接到.NET服务器并以纯json检索响应。
One possibility is to avoid using JSON altogether and use cURL and a PHP proxy instead. 一种可能是避免完全使用JSON,而改用cURL和PHP代理。 Send the data to a PHP script on your server, which then forms a cURL session with the script on the other domain and returns to result to the original AJAX caller.
将数据发送到服务器上的PHP脚本,然后与另一个域上的脚本形成cURL会话,并返回结果给原始AJAX调用者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.