net webservice ajax访问


    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    <webServices>
      <protocols>
        <add name= "HttpPost"/>
        <add name= "HttpGet"/>
        <add name="HttpSoap"/>
        <add name="Documentation"/>

      </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"/>
        <add name="Access-Control-Allow-Origin" value="*" /> 
      </customHeaders>
    </httpProtocol>
  </system.webServer>
  


/// <summary>
///WebService 的摘要说明http://localhost:65497/
/// </summary>
//[WebService(Namespace = "http://tempuri.org/")]
[WebService(Namespace = "http://localhost:65497/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
        
    }

    [WebMethod]
    public string HelloWorld() {
        Context.Response.AppendHeader("Access-Control-Allow-Methods", "OPTIONS,POST,GET");
        Context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");
        Context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
        return "Hello World";
    }
    [WebMethod]
    public string GetAge(string id)
    {
        Context.Response.AppendHeader("Access-Control-Allow-Methods", "OPTIONS,POST,GET");
        Context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");
        Context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
      Context.Response.AppendHeader("Content-type", "application/json");
        return "ID为:" + id + "的年龄为:" + new Random().Next(10, 41);
    }
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" src=" http://libs.baidu.com/jquer77777y/1.11.1/jquery.min.js "></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#getdata").click(function () {
                    /*
                    $.ajax({
                    type: 'Post',
                    url: 'http://localhost:65497/WebSite1/WebService.asmx/GetAge?id=3344',
                    // data: '{id:"bbut101"}',
                    //  data: 'id=bbut101',
                    // dataType: 'json',
                      dataType: 'xml',
                    contentType: "application/json",
                    success: function (data) {
                    $("#data").append(data.d);
                    }
                    });
                    
*/
                    // alert("3333333333");

                    //  htmlobj = $.ajax({ url: "http://localhost:65497/WebSite1/WebService.asmx/GetAge", async: false });
                    //  $("#data").html(htmlobj.responseText);
                    /*
                    //  $.post("http://localhost:65497/WebSite1/WebService.asmx/GetAge",
                    {  id: "Duckburg"    },
                    function (data, status) {
                    alert("数据:" + data + "\n状态:" + status);
                    }); 
                    
*/
                    /*
                    $.ajax({
                    type: "GET",
                    contentType: "UTF-8",
                    url: "http://localhost:65497/WebSite1/WebService.asmx/GetAge?id=3322",
                    dataType: 'jsonp',
                    // dataType: 'text',
                    success: function (data) {
                    alert(data);
                    }
                    });
                   
                    $.ajax({
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    url: "http://localhost:65497/WebSite1/WebService.asmx/GetAge?id=44333",
                    data: "{id:12345 }",
                    dataType: 'jsonp',
                    success: function (response) {
                    alert("成功:" + response.d);
                    },
                    error: function (msg) {
                    alert("错误:" + msg);
                    }
                    })
                     
*/
                   // jQuery.support.cors = true; //IE10以下
                    $.ajax({
                        type: "post",
                        url: "http://localhost:65497/WebSite1/WebService.asmx/GetAge?id=44333",
                       // dataType: 'xml',
                        dataType: 'text',
                        data: { id: 'eve7777' },
                        success: function (data) {
                            alert(data); //xml对象
                           // document.write(data);
                          //  $("#data").append(data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            // console.log(XMLHttpRequest);
                            alert('error:' + errorThrown);
                           // $("#data").append(errorThrown);
                        }
                    });
                   

                });
            });
        </script>
</head>
<body>
    <a id="getdata" href="javascript:void(0);">获取dddd数据</a>
    <div id="data"></div>
</body>
</html>



posted on 2020-12-17 16:58 青蛙學堂 阅读(210) 评论(0)  编辑 收藏 引用 所属分类: c#JavaScript

只有注册用户登录后才能发表评论。
<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜