javascript - 如何通过邮寄方式发送 json
javascript - how to send json by postmessage
我有一个 C# 字符串变量具有以下序列化的 Json 字符串:
{
"Video": "1",
"Voice": "1"
}
我正在尝试通过 postMessage
post 像这样:
string jsonVerticalTypeQuantity = Newtonsoft.Json.JsonConvert.SerializeObject(VerticalTypeQuantity);
<script>
$(document).ready(function () {
parent.postMessage({ "SelectedComponent": "@jsonVerticalTypeQuantity"}, "*");
});
</script>
但是当我在浏览器中检查它时,它会添加额外的字符,例如 "
为什么会这样?以及如何 post JSON 字符串原样?
@
指令在针对服务器端 Razor 变量使用编码 HTML 时自动对输出字符串进行编码。你应该把 @Html.Raw()
helper 放到 return unencoded JSON string:
parent.postMessage({ "SelectedComponent": @Html.Raw(jsonVerticalTypeQuantity) }, "*");
或者使用一个变量作为替代:
@{
string jsonVerticalTypeQuantity = Newtonsoft.Json.JsonConvert.SerializeObject(VerticalTypeQuantity);
}
<script>
$(document).ready(function () {
var jsonData = @Html.Raw(jsonVerticalTypeQuantity);
parent.postMessage({ "SelectedComponent": jsonData }, "*");
});
</script>
相关问题:
我有一个 C# 字符串变量具有以下序列化的 Json 字符串:
{
"Video": "1",
"Voice": "1"
}
我正在尝试通过 postMessage
post 像这样:
string jsonVerticalTypeQuantity = Newtonsoft.Json.JsonConvert.SerializeObject(VerticalTypeQuantity);
<script>
$(document).ready(function () {
parent.postMessage({ "SelectedComponent": "@jsonVerticalTypeQuantity"}, "*");
});
</script>
但是当我在浏览器中检查它时,它会添加额外的字符,例如 "
为什么会这样?以及如何 post JSON 字符串原样?
@
指令在针对服务器端 Razor 变量使用编码 HTML 时自动对输出字符串进行编码。你应该把 @Html.Raw()
helper 放到 return unencoded JSON string:
parent.postMessage({ "SelectedComponent": @Html.Raw(jsonVerticalTypeQuantity) }, "*");
或者使用一个变量作为替代:
@{
string jsonVerticalTypeQuantity = Newtonsoft.Json.JsonConvert.SerializeObject(VerticalTypeQuantity);
}
<script>
$(document).ready(function () {
var jsonData = @Html.Raw(jsonVerticalTypeQuantity);
parent.postMessage({ "SelectedComponent": jsonData }, "*");
});
</script>
相关问题: