Jquery 将字符串转换为$.post()的键/值对
我希望得到与这行代码相同的结果:Jquery 将字符串转换为$.post()的键/值对,jquery,Jquery,我希望得到与这行代码相同的结果: $.post("test.php", { name: "John", time: "2pm" } ); $.post("test.php", str ); 如何转换字符串变量 var str = '{ name: "John", time: "2pm" }'; 进入键/值对,以便我可以使用以下代码行发送POST请求: $.post("test.php", { name: "John", time: "2pm" } ); $.post("test
$.post("test.php", { name: "John", time: "2pm" } );
$.post("test.php", str );
如何转换字符串变量
var str = '{ name: "John", time: "2pm" }';
进入键/值对,以便我可以使用以下代码行发送POST请求:
$.post("test.php", { name: "John", time: "2pm" } );
$.post("test.php", str );
谢谢 jQuery有一个专门用于此的内置方法
如果需要将字符串转换为JSON对象,请尝试以下操作:
jQuery.parseJSON(str );
因此,您的代码如下所示:
$.post("test.php", jQuery.parseJSON(str ));
文档:使用$.ajax:
$.ajax({
type: "POST",
url: "test.php",
data: str,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(e) {
//Function here onsucess
}
});