2011-10-28 26 views
2

我想有相同的结果,因为这行代码键/值对:

$.post("test.php", { name: "John", time: "2pm" }); 

如何转换一个字符串变量

var str = '{ name: "John", time: "2pm" }'; 

成键/值对,这样我可以用这行代码发送POST请求:

$.post("test.php", str); 

谢谢!

回答

0

如果你需要一个字符串转换成JSON对象试试这个:

jQuery.parseJSON(str); 

使你的代码看起来像:

$.post("test.php", jQuery.parseJSON(str)); 

documentation:http://api.jquery.com/jQuery.parseJSON/

0

使用$阿贾克斯:

$.ajax({ 
    type: "POST", 
    url: "test.php", 
    data: str, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(e) { 
     //Function here onsucess    
    } 
});