错误 32600:JSON-RPC 请求必须是一个对象
Error 32600: JSON-RPC Request must be an object
我的要求是:
var req = {"action": "UserAPI","method": "Authenticate","data": ["un","pw"],"type": "rpc", "tid": "1"}
$.post("http://localhost/myServer/RPC/ROUTER",req, function(result){
console.log(result.responseText);
});
响应是:
"{ "jsonrpc" : "2.0", "error" : { "code" : -32600, "message" : "JSON-RPC Request must be an object." }, "tid" : null }"
请求已经是一个对象。这有什么问题吗?
如果服务器需要一个 JSON-RPC 请求,那么您需要按照它想要的方式格式化您的请求。
JSON-RPC 表示服务器需要一个 JSON 字符串作为 POST 正文,而不是像您发送的表单数据。
试试这个:
var req = {
"action": "UserAPI",
"method": "Authenticate",
"data": ["un","pw"],
"type": "rpc",
"tid": "1"
};
$.ajax({
url: "http://localhost/myServer/RPC/ROUTER",
type: 'post',
// This tells the server we are sending
// JSON data as the payload
contentType: 'application/json',
// Encode the object as a JSON string
data: JSON.stringify(req),
// The response is JSON
dataType: 'json',
success: function(result){
// This should be parsed for you, so `result` will be an object
console.log(result);
}
});
我的要求是:
var req = {"action": "UserAPI","method": "Authenticate","data": ["un","pw"],"type": "rpc", "tid": "1"}
$.post("http://localhost/myServer/RPC/ROUTER",req, function(result){
console.log(result.responseText);
});
响应是:
"{ "jsonrpc" : "2.0", "error" : { "code" : -32600, "message" : "JSON-RPC Request must be an object." }, "tid" : null }"
请求已经是一个对象。这有什么问题吗?
如果服务器需要一个 JSON-RPC 请求,那么您需要按照它想要的方式格式化您的请求。
JSON-RPC 表示服务器需要一个 JSON 字符串作为 POST 正文,而不是像您发送的表单数据。
试试这个:
var req = {
"action": "UserAPI",
"method": "Authenticate",
"data": ["un","pw"],
"type": "rpc",
"tid": "1"
};
$.ajax({
url: "http://localhost/myServer/RPC/ROUTER",
type: 'post',
// This tells the server we are sending
// JSON data as the payload
contentType: 'application/json',
// Encode the object as a JSON string
data: JSON.stringify(req),
// The response is JSON
dataType: 'json',
success: function(result){
// This should be parsed for you, so `result` will be an object
console.log(result);
}
});