WCF解析jason返回的小实例

首先需要在后台返回Json格式的数据,.net中处理Json数据的API,添加一个dll的链接库,如图:


后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Newtonsoft.Json;

namespace WCFTest2
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WCFservice”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WCFservice.svc 或 WCFservice.svc.cs,然后开始调试。
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
    public class WCFservice 
    {
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public string InsertRow(int id, string title, string content)
        {           
            string data = string.Format("您输入的标题是:{0}<br/><br/>您输入的内容是:{1}<br/><br/>此文章的id是:{2}", title, content, id.ToString());           
            var jsonStr = JsonConvert.SerializeObject(data);
            return jsonStr;
        }
    }
}

前端代码:

<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 src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
   
     <script type="text/javascript">
         function callServer(){
             var id = Number($("#id").val());
             var title = String($("#title").val());
             var content = String($("#content").val());
             $.ajax({
                 type: 'post',
                 url: '/WCFservice.svc/InsertRow',
                 contentType: 'text/json',
                 data: '{"id":' + id + ',"title":"' + title + '","content":"' + content + '"}',
                 success: function (data) {
                     $.each(data, function (i, n) {
                         alert(n);
                         $("#divContent").prepend("<p>"+n+".</p> ");
                     });
                 }
             });
         }
     </script> 
    <style type="text/css">
            #content{height: 181px;width: 549px;}
            #title{width: 544px;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    文章标题:<input type="text" id="title" />
        <br />
    文章内容:<textarea id="content"></textarea>
    <input type="hidden" id="id" value="1" /><br />
    <input type="button"  value="提交" οnclick="callServer();" />
    </div><br />
     <div id="divContent"></div>
      <p></p>
    </form>
</body>
</html>

config配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AllenBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WCFTest2.WCFservice">
        <endpoint address="" behaviorConfiguration="AllenBehavior" binding="webHttpBinding" contract="WCFTest2.WCFservice" />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
        在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

实现效果:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值