2013-04-04 22 views
3
$.get ("/asd", { 
    asd: "foo" 
}, function (res){ 
    alert ("ok"); 
}); 

這將參數發送的查詢字符串:如何使AJAX與HTTP身體參數GET

/asd?asd=foo 

但我只需要使用HTTP主體發送JSON對象:

{ "asd": "foo" } 
+0

正文將在POST請求。嘗試使用jQuery.post()。但身體將在urlencoded字符串。 – Victor 2013-04-04 11:36:31

+0

但我需要發送一個GET請求。 – 2013-04-04 11:38:33

+0

我發現:http://stackoverflow.com/questions/10298899/how-to-send-data-in-request-body-with-a-get-when-using-jquery-ajax – Victor 2013-04-04 11:45:41

回答

0

如果你想發送一個JSON對象,那麼你應該真的使用POST。但是,如果你絕對需要使用GET,你可以這樣做:

$.get ("/asd", { json: JSON.stringify({asd: "foo"}) }, function (res){ 
    alert ("ok"); 
}); 

這將JSON對象轉換爲字符串,並將其傳遞到json GET參數(要謹慎,JSON.stringify不起作用在舊的瀏覽器) 。