我需要將一個字串發送到一個接受 JSON 資料的端點
data = '{"data":"' some_data '"}';
$.ajax({
type: 'POST',
url: "example.com/endpoint",
data: data,
success: function(data) { console.log('data: ' data); },
contentType: "application/json",
dataType: 'json'
});
現在,當this is a text with " random text " , to test作為 ajax 請求的值提供時some_data,會在控制臺中引發錯誤,但在提供this is a text with \" random text \" , to test 時作業正常
有沒有一種內置的方法可以"在\"不撰寫我自己的函式的情況下進行轉換
uj5u.com熱心網友回復:
當你能做到的時候為什么要那樣做
let data = {data: ' this is a text with " " ' };
data = JSON.stringify(data);
console.log(data);
只需使用 JSON.stringify
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/410721.html
標籤:
下一篇:根據資料庫創建下拉串列
