C# 使用 Jquery Ajax 将 html 字符串传递给服务器端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18731559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass html string to server side with Jquery Ajax
提问by Mr. Lost
i have seen a lot of answers in this site that have helped me a lot but in this one i need to ask guys to help me out.
我在这个网站上看到了很多对我有很大帮助的答案,但在这个网站上我需要请人帮助我。
i have a textarea as a Html editor to pass html content to the server and append it to a newly created Html page( for user POST,etc), but jquery or ASP.NET does not accept the Html content passed by jquery through data: {}
我有一个 textarea 作为 Html 编辑器将 html 内容传递到服务器并将其附加到新创建的 Html 页面(用于用户 POST 等),但 jquery 或 ASP.NET 不接受 jquery 通过数据传递的 Html 内容: {}
--For Jquery:
--对于Jquery:
$("#btnC").click(function (e) {
e.preventDefault();
//get the content of the div box
var HTML = $("#t").val();
$.ajax({ url: "EditingTextarea.aspx/GetValue",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{num: "' + HTML + '"}', // pass that text to the server as a correct JSON String
success: function (msg) { alert(msg.d); },
error: function (type) { alert("ERROR!!" + type.responseText); }
});
and Server-Side ASP.NET:
和服务器端 ASP.NET:
[WebMethod]
public static string GetValue(string num)
{
StreamWriter sw = new StreamWriter("C://HTMLTemplate1.html", true);
sw.WriteLine(num);
sw.Close();
return num;//return what was sent from the client to the client again
}//end get value
Jquery part gives me an error:
Invalid object passed in and error in
System.Web.Script.Serialization.JavascriptObjectDeserializer.
Jquery 部分给了我一个错误:传入的对象无效,传入错误
System.Web.Script.Serialization.JavascriptObjectDeserializer.
It's like jquery doesnt accept string with html content.what is wrong with my code ?
这就像 jquery 不接受带有 html 内容的字符串。我的代码有什么问题?
采纳答案by Subin Jacob
Pass it like this
像这样传递
JSON.stringify({'num':HTML});
You have to stringify the content to JSON properly. HTML may contain synataxes that would make the JSON notation invalid.
您必须正确地将内容字符串化为 JSON。HTML 可能包含会使 JSON 表示法无效的语法。
var dataToSend = JSON.stringify({'num':HTML});
$.ajax({ url: "EditingTextarea.aspx/GetValue",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: dataToSend , // pass that text to the server as a correct JSON String
success: function (msg) { alert(msg.d); },
error: function (type) { alert("ERROR!!" + type.responseText); }
});
回答by Bibhu
You can use this
你可以用这个
var HTML = escape($("#t").val());
and on server end you can decode it to get the html string as
在服务器端,您可以对其进行解码以获取 html 字符串
HttpUtility.UrlDecode(num, System.Text.Encoding.Default);
回答by user3942119
Make sure JSON.stringify,dataType: "json" and, contentType: "application/json; charset=utf-8", is there in the ajax call.
确保JSON.stringify,dataType: "json" 和 contentType: "application/json; charset=utf-8",是否存在于 ajax 调用中。
[HttpPost]
public ActionResult ActionMethod(string para1, string para2, string para3, string htmlstring)
{
retrun view();
}
$.ajax({
url: '/Controller/ActionMethod',
type: "POST",
data: JSON.stringify({
para1: titletext,
para2: datetext,
para3: interchangeNbr.toString(),
htmlstring : messageText.toString()
}),
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
success: function successFunc(data) {
},
error: function errorFunc(jqXHR) {
$('#errroMessageDiv').css({ 'color': 'red' }).text(jqXHR.message).show();
}
});