解決jquery調用NET webservice跨域的問題

來源:互聯網
上載者:User

標籤:res   tin   option   .config   query   .com   requested   需要   .net   

聲明,解決方案由網上收集而來,個人整理。有別人的,也有我的。

一、webserive端

1.web.config

需要在web.config的configuration節點中加入如下的黑體部分內容。

  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
  </system.web>

再加入以下內容

  <system.webServer>
    <!--解決跨域請求 by wys -->
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET" />
        <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type" />
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

 

2.asm.x

還不清楚是否必須在類前要加上以下內容。沒時間測

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]

 

3.一個方法

        [WebMethod]
        public string getProduct()
        {
            DataTable dt = BizLogic.ProductsBLL.GetProductInfo();
            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

            var obj = new
            {
                Status = 1,
                Data = dt
            };
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }

二。JS部分

var svr_url = "http://192.168.2.23:9001/******.asmx/";

function getdata(action,para,callback)
{
    var uu = svr_url + action;
    $.ajax({
        type: "post",
        contentType:"application/json",
        data:para,
        url:svr_url + action,
        dataType:‘json‘,
        success:function(result){  
            console.log(result);
            var res=JSON.parse(result.d);
            if(res.Status==1){
                if(callback)callback(res.data);
            }
            else
            {
                alert(res.Message);
            }
        },
        error:function(err){
            console.log(err);
        }
    });  
}

 

三。HTML頁面

  getdata(‘getProduct‘,[],afterGet); 

  var afterGet=function(data)

  {

    ......

  }

 

 

以上實測通過,系統運行中。

 

解決jquery調用NET 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.