jquery ajax跨域請求webservice

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

有種方式可以通過JSONP方式來請求

這裡具體介紹如何通過修改設定檔來實體AJAX跨域請求WEBSERVICE

 

 WEBSERVICE的類聲名

    /// <summary>    /// MobileService 的摘要說明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。    [System.Web.Script.Services.ScriptService]    public class TestService : System.Web.Services.WebService    {    }

WEBCONFIG的修改

  開啟允許POST GET請求

 <add name="HttpGet"/> 
<add name="HttpPost"/>
開啟允許跨域請求
      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>      <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>      <!--Value="*"不限制網域名稱 -->      <add name="Access-Control-Allow-Origin"  value="http://domain1.com, http://domain2.com" />
 
<configuration>    <system.web>      <compilation debug="true" targetFramework="4.0" />      <webServices>        <protocols>          <add name="HttpGet"/>          <add name="HttpPost"/>        </protocols>      </webServices>          </system.web>  <system.webServer>    <httpProtocol>    <customHeaders>      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>      <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>      <!--Value="*"不限制網域名稱 -->      <add name="Access-Control-Allow-Origin"  value="http://domain1.com, http://domain2.com" />    </customHeaders>  </httpProtocol>  <modules>    <add name="MyHttpModule" type="WebServiceDemo.MyHttpModule"/>  </modules>  </system.webServer></configuration>

AJAX範例程式碼


$.ajax({
                type: "POST",
                url: "http://10.10.0.1:8888/XXXXX.asmx/XXXXX",
                dataType: "JSON",
                contentType: "application/json",
                data: { Data1: "HELLO", Data2: "WORD" },
                success: function (msg) {
                    alert(msg);
                }
            });

jquery ajax跨域請求webservice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.