• jquery ajax跨域请求webservice


    有种方式可以通过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);
                    }
                });

  • 相关阅读:
    用csc命令行手动编译cs文件
    笔录---果壳中的C#第一章
    Visual Studio2012快捷键总结
    JavaScript 二维数组排列组合2
    JavaScript 递归法排列组合二维数组2
    JavaScript 递归法排列组合二维数组
    JavaScript 二维数组排列组合
    在 CentOS6 上安装 GraphicsMagick-1.3.30
    Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Unable to load the mojo 'resources' (or one of its required components)
    java.sql.SQLException: Column count doesn't match value count at row 1
  • 原文地址:https://www.cnblogs.com/enya1999/p/4134027.html
Copyright © 2020-2023  润新知