ajax 向控制器发请求 data中带有富文本 及特殊字符导致乱码

20 篇文章 0 订阅
8 篇文章 0 订阅

页面:

@{
    Layout = null;
}
<!DOCTYPE html>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<html>
<head>
    <meta charset="utf-8"/>
    <title>前端发送json,后台实体类接受</title>
</head>
<body>
    <input type="button" id="name" value="点击发送" />
    <script>
        var datas= {
            Id: '81E6F804-67AF-4034-BCF5-02AA696C8418',
            Name: '遗体搬运服务2',
            CateCode: '0102',
            Ptype: '0',
            Sales: '10',
            SortNum: '0',
            IsActive: '0',
            Memo: encodeURI('<a>hhda</a>'),
            StationId: '81E6F804-67AF-4034-BCF5-02AA696C8418'
        }
        $('#name').on('click', function (obj) {
            $.ajax({
                url: '@Url.Action("GetDate","Yinshe")',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data: datas,
                type: 'POST',
                //traditional: true,
                success: function (e) {
                    alert(e)
                },
                error: function (e) {
                    alert('失败')
                }
            })
        })

    </script>
</body>
</html>

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CreateId.Controllers
{
    public class YinsheController : Controller
    {
        // GET: Yinshe
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetDate(P_Product obj) {
            var hh = obj.Memo;
            var res=  HttpUtility.UrlDecode(hh);
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        public partial class P_Product
        {
            #region Primitive Properties
            public virtual System.Guid Id
            {
                get;
                set;
            }
            public virtual string Name
            {
                get;
                set;
            }
            public virtual string CateCode
            {
                get;
                set;
            }
            public virtual int Ptype
            {
                get;
                set;
            }
            public virtual decimal Sales
            {
                get;
                set;
            }
            public virtual int SortNum
            {
                get;
                set;
            }
            public virtual int IsActive
            {
                get;
                set;
            }
            public virtual string Memo
            {
                get;
                set;
            }
            public virtual System.Guid StationId
            {
                get;
                set;
            }
            #endregion

        }

    }
}

方法:在js中对带特殊字符的字符串进行

encodeURI('<a></a>')    

在  控制器中得到


这不是我们想要的:

进行

 var res=  HttpUtility.UrlDecode(hh);  得到
'
<a></a>
'


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值