使用jQuery在远程服务器上调用Web服务 [英] Calling a web service on a remote server with jQuery

查看:158
本文介绍了使用jQuery在远程服务器上调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上停留了几个小时,无法找到有效的答案,可能是因为我对此类事情缺乏了解/经验.

I have been stuck on this issue for hours now, unable to find a valid answer, perhaps because of my lack of knowledge/experience with such things.

我知道由于Same-Origin的限制,我无法使用XMLHttpRequest在远程服务器上加载Web服务,但是显然可以使用jsonp数据类型并提供回调函数来完成,但是我真的不知道如何使它起作用.

I am aware that I cannot load a web service on a remote server with XMLHttpRequest due to Same-Origin restriction, but apparently this can be done with jsonp data type and providing a callback function but I really can't figure out how to make it work.

远程服务器是我的,所以我真的不在乎远程服务器可以发回任何恶意javascript代码的可能性.

The remote server would be mine, so I don't really care about the possiblity that the remote server can send back any malicious javascript code.

如果有人可以帮助我在 http://www.antoinecloutier.com/WebService1.asmx/HelloWorld

I would appreciate if anyone could help me to get some working code with the web service at http://www.antoinecloutier.com/WebService1.asmx/HelloWorld

我已经在服务器上创建了一个测试页,并且Web服务运行正常-> http://antoinecloutier. com/test.html

I have created a test page on my server and the web services works fine --> http://antoinecloutier.com/test.html

现在,我需要能够从其他服务器访问该Web服务.

Now, I need to be able to access that web service from a different server.

谢谢!

推荐答案

您可以通过启用CORS来创建Cross Domain AJAX Calls using jquery

You can make Cross Domain AJAX Calls using jquery by enabling CORS

例如:

 jQuery.support.cors = true;
    $.ajax({
        url: 'http://crossdomainurl',
        type: 'GET',
        dataType: 'json', 
        contentType: "application/json; charset=utf-8",           
        success: function (data) {                
            WriteResponse(data);
        },
        error: function (x, y, z) {
            alert(x + '\n' + y + '\n' + z);
        }
  });

要确保跨域正常工作,您必须在Web服务响应中添加Access-Control-Allow-Origin: *响应标头.

To Make Sure Cross Domain Work Properly you will have to add a Access-Control-Allow-Origin: * response header to the web service response.

在.NET WCF服务中,您可以按如下所示将其添加到服务的global.asax中,

In a .NET WCF service, you can add this to the global.asax of your service as follows,

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","*");
}

在PHP中,您可以按以下方式修改响应,

In PHP you can modify the response as follows,

<?php header('Access-Control-Allow-Origin: *'); ?>

Note:

发送Access-Control-Allow-Origin标头可以进行基本的跨域访问,但是调用ASP.NET服务(如ASMX ScriptServices,ASPX页面方法和启用了AspNetCompatibilityRequirements的WCF服务)要求您的站点以另外一个CORS标头响应:Access-Control-Allow-Headers

Sending the Access-Control-Allow-Origin header allows basic cross-origin access, but calling ASP.NET services like ASMX ScriptServices, ASPX page methods, and WCF services with AspNetCompatibilityRequirements enabled requires your site to respond with one additional CORS header: Access-Control-Allow-Headers

您可以在web.config中进行如下配置,

You can configure the same in web.config as follows,

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
    </customHeaders>
   </httpProtocol>
</system.webServer>

这篇关于使用jQuery在远程服务器上调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆