如果您使用的是 AJAX,您可以传递一个Object Literal或使用 de 数据构建查询字符串:例如
//if the variable contains HTML string,
//ensure to encode it before sending the request
var note = " <strong>Important:</strong><br/> Additional notes. ";
note = encodeHtml(note);
//Object Literal
var params = { "date": date, "expiry": expiry, "priority": priority, "note": note };
//Query string to append to the url
var paramsUrl = buildUrlParams(params).join("&");
$.ajax({
type: "POST",
url: "myservice/client",
//ugly way
//data: "date="+date+"&expiry="+expiry+"&priority="+priority+"¬e="+note,
//passing data as Literal Object
data: params,
//passing data as querystring
//data: paramsUrl,
success: function(data){ alert(data); }
});
//Creates an array of parameters to build an URL querystring
//@obj: Object to build the array of parameters
function buildUrlParams(obj) {
return $.map(obj, function(value, key) {
return key + "=" + value;
});
}
//Encodes the HMTL to their respective HTML entities
//@text: the HTML string to encode
function encodeHtml(text) {
var div = document.createElement("div");
if ("innerText" in div) div.innerText = text;
else div.textContent = text;
return div.innerHTML.replace(/^\s+|\s+$/, "");
}