encodeURIComponent怎么用?
我想把 var str中的参数通过ajax 传到后台中,但是如果内容里有换行或者tab的时候就是提示传入的对象无效,应为“:”或“}”。
我从网上查到使用encodeURIComponent,但是如果var jsoncotent=encodeURIComponent(str); 那么在后台怎么才能解析其中的参数呢?前台后后台代码如下,请各位指点一下
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
case "update": {
var editor = $('#elm1').xheditor({ tools: 'full', skin: 'default' });
var countert = editor.getSource();
if (imgPath == "") {
imgPath = null;
var str = '{"cTitle":"' + $("#title").val() + '","cContent":"' + countert + '","cImage":"' + imgPath + '","id":"' + thisID + '"}';
}
else {
var str = '{"cTitle":"' + $("#title").val() + '","cContent":"' + countert + '","cImage":"' + imgPath + '","id":"' + thisID + '"}';
}
alert(jsonContent);
$.ajax({ //一个Ajax过程
type: "post", //以post方式与后台沟通
contentType: "application/json",
url: "AfterService.aspx/upData", //与此php页面沟通
dataType: 'json',//从php返回的值以 JSON方式 解释
data: str,
error: function (msg) {
alert(msg.responseText);
},
success: function (mag) {
var str = mag.d;
var json = JSON.parse(str);
alert(json.msg);
}
});
} break;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static string upData(string cTitle, string cContent, string cImage, string id)
{
DataBase db = new DataBase();
int ID = Convert.ToInt32(id);
string sql = "update Content set cTitle=@cTitle,cContent=@cContent,cImgPatch=@cImage where id=@id";
SqlParameter[] sq = new SqlParameter[4];
sq[0] = new SqlParameter("@cTitle", cTitle);
sq[1] = new SqlParameter("@cContent", cContent);
sq[2] = new SqlParameter("@cImage", cImage);
sq[3] = new SqlParameter("@id", ID);
if (db.runSql(sql,sq)!=0)
{
return "{\"msg\":\"保存成功!\"}";
}
else
{
return "{\"msg\":\"保存失败!\"}";
}
}