zoukankan      html  css  js  c++  java
  • jquery ajax CORS 跨域訪问 WebService

    JS代码:

    var word = document.getElementById("word").value;
    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        url: "http://localhost:12805/WebService.asmx/HelloWorld",
        data: 'data=' + word,
        dataType: 'text',
        success: function(result) {
            alert("success: " + result);
        },
        error: function(result, status) {
            alert("error: " + status);
        }
    });
    

    C# WebService代码:

    [WebService(Namespace = "weburl")]
    [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]
    public class WebService : System.Web.Services.WebService
    {
        [WebMethod]
        public void HelloWorld(string data)
        {
            this.Context.Response.AddHeader("Access-Control-Allow-Origin", "*"); // CORS 跨域訪问
            this.Context.Response.Write(data); // 返回字符串格式
        }
    }
    

    WebService 默认 return 返回 XML 格式,可是使用上述方式是以字符串形式返回。


    
    

  • 相关阅读:
    PatentTips
    PatentTips
    PatentTips
    PatentTips – Java native function calling
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
    PatentTips
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7106372.html
Copyright © 2011-2022 走看看