您好,我正在对 RESTful Rails 资源执行 GET 请求,如下所示:
function getGroups(category) {
$.ajax({
type: 'GET',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "category":category }),
success: function(data) {alert(data)},
contentType: "application/json",
dataType: 'json'
});
});
getGroups("own_groups");
问题是 Webrick 服务器出现如下错误:
ERROR bad URI `/groups.json?{%22access_token%22:%22569669d8df0456%22,%22category%22:%22own_groups%22}'.
它一定与 JSON 数据的解析方式有关,因为我对另一个不带 JSON 数据的 GET 请求和一个带 JSON 数据的 POST 请求没有任何问题...
更新:添加 POST 请求代码(需要 JSON.stringify)
function addGroup(name, description) {
$.ajax({
type: 'POST',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "group_name":name, "group_description":description}),
success: function(data) { alert("ok")},
contentType: "application/json",
dataType: 'json'
});
};
addGroup("nice group", "full of people");
最佳答案
不要使用JSON.stringify
。简单地说:
data: {"access_token":"569669d8df0456", "category":category },
此外,您不需要指定完整的url http://localhost:3000/groups.json
,它可以只是简单的groups.json
关于javascript - 错误: bad URI when sending data as JSON with GET method to a Rails resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26663809/