ajax 調用 webService

來源:互聯網
上載者:User

標籤:程式   請求   json格式   允許   sip   java   bind   uri   pac   

前台指令碼
<script type="text/javascript">        $(function () {            $.ajax({                type: "POST", //訪問WebService使用Post方式請求                contentType: "application/json", //請求WebService 返回的資料類型(預設是xml,這裡聲明成了Json)                url: "WebService.asmx/ReverseString", //調用WebService的地址和方法名稱組合 ---- WsURL/方法名                data: "{ value1: ‘明天到操場操到天明‘ }", //這裡是要傳遞的參數,格式為 data: "{paraName:paraValue}"                dataType: ‘json‘, //請求webService時,給webService傳遞資料的格式。                success: function (result) {//回呼函數,result,傳回值                    alert(result.d);                }            });        });    </script>

前台代碼注意點:

  1、 聲明了傳回值和傳輸值為JSON格式的時候,傳遞的data應該是json格式的字串,否則chrome會報錯

  2、關於result.d,很多部落格都沒有說明。返回的json字串的key之所以是d,是為了確保安全(防止XSS攻擊)(http://blog.sina.com.cn/s/blog_6c762bb30100wqs9.html)

後台代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace Demo{    /// <summary>    /// WebService 的摘要說明    /// </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 WebService : System.Web.Services.WebService    {        [WebMethod]        public string ReverseString(string value1)//這個方法的參數一定要和ajax的data中的key的名字相同,這裡是value1,ajax中data的key也是value1        {            char[] arr = value1.ToCharArray();            Array.Reverse(arr);            value1 = new string(arr);            return value1;                       //返回的string會自動轉換成JSON?                   }    }}

 如果是本項目中ajax請求後台,那麼優先推薦使用一般處理常式。

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.