我的要求是:
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 }"
请求已经是一个对象。这有什么问题?
答案 0 :(得分:3)
如果服务器需要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);
}
});