当字符串包含HTML标记时,如何发送JSON POST [英] How can I send a JSON POST when the string contains HTML tags

查看:157
本文介绍了当字符串包含HTML标记时,如何发送JSON POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道变量'LastSelectedSettingValue'有时会包含html标签。

当它是简单文本时,下面的JSON工作正常。当它包含HTML时,下面的JSON失败。我尝试过JSON.Encode()和其他东西,但没有成功。



I know that the variable 'LastSelectedSettingValue' will sometimes contain html tags.
When it is simple text the JSON below works fine. When it contains HTML the JSON below fails. I have tried JSON.Encode() and other things, but without success.

$.ajax({
               url: '/StoreAdmin01/UpdateSetting',
               type: 'POST',
               dataType: "json",
               traditional: true,
               async: false,
               data: { settingId: LastSelectedSettingIdValue, settingValue: LastSelectedSettingValue, time: new Date() },
               success: function (result) {
               },

               error: function () {
                   alert("error");

               }
           });

推荐答案

.ajax({
url:' / StoreAdmin01 / UpdateSetting'
type:' POST'
dataType: json
traditional:true,
async:false,
data:{settingId:LastSelectedSettingIdValue,settingValue:LastSelectedSettingValue,time:new Date ()} ,
成功:功能(结果){
},

错误: function (){
alert( error);

}
});
.ajax({ url: '/StoreAdmin01/UpdateSetting', type: 'POST', dataType: "json", traditional: true, async: false, data: { settingId: LastSelectedSettingIdValue, settingValue: LastSelectedSettingValue, time: new Date() }, success: function (result) { }, error: function () { alert("error"); } });


使用Base64转码器对字符串进行编码:

https://code.google.com/p/javascriptbase64/ [ ^ ]

喜欢这样:

Use Base64 transcoder to encode your string:
https://code.google.com/p/javascriptbase64/[^]
Like this:
Base64.encode('yourvalue') 



在服务器端,使用:


In Server side, use:

byte[] b = Convert.FromBase64String(yourpostedvalue);
string yourvalue = System .Text.Encoding.UTF8.GetString(b);


这篇关于当字符串包含HTML标记时,如何发送JSON POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆