使用jQuery调用远程ASMX的问题

我一直在努力正确理解这一点。 XML,SOAP和JSON响应之间有什么区别? 如何知道如何调用响应为上述之一的Web服务? (…如果我偏离轨道,请纠正我)

我问这个的原因是因为我试图在我的.NET3.5 webapp中从jQuery调用远程ASMX,而且没有运气! 基本上我试图调用一个CurrencyConverter方法,如下所示: http : //www.webservicex.net/CurrencyConvertor.asmx?op = ConversionRate

我可以看到它返回XML,但以下代码不起作用:

$('#Currency').bind('change', function() { var targetDiv = '#Result' var currencyValue = $('#Currency option:selected').attr('value') var webMethod = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate' var parameters = "{'FromCurrency':'GBP','ToCurrency':'" + currencyValue + "'}" $(targetDiv).html('loading...'); $.ajax({ type: "POST", url: webMethod, data: parameters, contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { $(targetDiv).html(response.d); }, error: function(response) { $(targetDiv).html("Unavailable:" + response); } }); }); 

因为我真的迷路了,请有人可以帮我解决这个问题!

谢谢!

我之前使用过这个Web服务。 它期望并返回XML。 这是我以前在Internet Explorer中工作的代码(对于Firefox,您需要使用jsonp)。

 $('#Currency').bind('change', function() { var targetDiv = '#Result' var currencyValue = $('#Currency option:selected').val(); var webMethod = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate'; var parameters = "?FromCurrency=GBP&ToCurrency=" + currencyValue; $(targetDiv).html('loading...'); $.ajax({ type: "GET", url: webMethod + parameters , contentType: "text/xml; charset=utf-8", dataType: "xml", //for Firefox change this to "jsonp" success: function(response) { $(targetDiv).html(response.text); }, error: function(xhr, textStatus, errorThrown) { $(targetDiv).html("Unavailable: " + textStatus); } }); )}; 

[编辑]你可以尝试的另一件事是将JQuery调用中的dataType更改为“xml”。 如果这不起作用,您可以创建自己的代理Web服务来调用远程代理,然后以JSON格式返回数据。

我怀疑问题出在服务器端代码中。 我不确定这是否适合你,但这里有一些工作代码,显示JQuery调用我的WebMethod。 希望你可以将它与你的产品进行比较并使其正常工作。 让我们知道解决方案是什么。 我希望这有帮助。

 [Server Code] using System; using System.ComponentModel; using System.Data; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] [ScriptService] public class ForumService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] public VoteCastResult CastQuestionVote(string itemID, QAForum.Bll.VoteType voteType) { try { User usr = SecurityHelper.GetOrCreateUser(); Guid question = new Guid(itemID); return new QuestionVoteController().CastQuestionVote(usr, question, voteType); } catch (Exception ex) { return new VoteCastResult(VoteCastStatusType.otherIssue, 0, ex.Message); } } } [JQuery Code] function AFTopicCastVote(clickContext, itemID, voteDirection, voteMethod) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: (AFServiceUrl + voteMethod), data: "{'itemID': '" + itemID + "','voteType': '" + voteDirection + "'}", dataType: "json", success: function (data, textStatus) { AFTopicProcessVoteResult(clickContext, data.d); //alert("data : " + data.d); }, error: function ( XMLHttpRequest, textStatus, errorThrown) { alert("error casting vote: " + errorThrown); } }); } 

在页面加载function中添加客户端的下一行….

  base.Response.AddHeader("Access-Control-Allow-Origin", "*"); base.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); base.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With"); base.Response.AddHeader("Access-Control-Max-Age", "86400"); 

问题与跨站点发布有关。 您可能会收到错误"Access to restricted URI denied code: 1012"因为您要发布到另一个域中的WebService。

请参阅错误1012上的这篇文章