简体   繁体   中英

WCF Rest Service to accept Json Data

我想将文本框数据转换为JSON格式,然后使用post方法将该JSON数据发布到wcf rest服务吗?

Simple put this code on your aspx page and use correct service url

  var WCF_Service_URL = "http://your_service_ip/Service_name.svc/YourMethod";
    var myData = $('txtMyTextBox').val();
    $.ajax({
        type: "POST",
        url: WCF_Service_URL, 
        data: JSON.stringify(myData), // here convert into json format
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: true,
        success: function (msg) {
            alert(msg);
        },
        error: function (ex) {
            alert(ex);
        }
    });



   // wcf methods
            // 1) your IServices.cs interface method 
            [OperationContract]
            [WebInvoke(
            ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST",
            UriTemplate = "YourMethod")]
            string YourMethod(string JsonData);

            // 2) your Services.cs  

              public string YourMethod(string JsonData)
        {

           string FirstName = JsonData.Firstname; // your json object data 

                  // save here into database 

            return "Save successfully";

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM