當字符串化字符串直接發送到請求時,它不會添加任何斜線。JSON stringify正在添加反斜槓
var data = { "A": "Aa", "B": "Bb", "C": "Cc" }; // This is JSON object
data = JSON.stringify(data); // Getting stringified
var obj = {method: "POST",
url: 'http://..XX..XXX.....com',
data: data // String is being sent as it is
};
$http(obj);// Have no slashes added
//Output: {"A":"Aa","B":"Bb","C":"Cc"}
但是,如果將字符串化的字符串設置爲對象的屬性值,並將對象發送到服務器,則字符串具有反斜槓。
var data = { "A": "Aa", "B": "Bb", "C": "Cc" };
data = JSON.stringify(data);
var obj = {method: "POST",
url: 'XXX',
data: { // String is being sent as a value of object property "Values"
"Values": data
}
};
$http(obj);//Slashes are added
//output: {"Values":"{\"A\":\"Aa\",\"B\":\"Bb\",\"C\":\"Cc\"}"}
有人可以看一看嗎?
由於您在第二個代碼片段中給出了一個Angular的對象及其'data'選項,所以這些值將被雙重編碼 - 一次由您和一次爲'$ http'。您可能不需要使用'JSON.stringify()'。 –