我正在使用RestClient将ajax请求转换为Ruby
Javascript中的函数
var dispatchWebAPIEndpointURL = 'http://dispatchweb.eureka-technology.fr/webmanager/WCFDispatchAPI.svc/REST/';
function APICallPOST(Method, Data, OnSucces, OnError) {
$.ajax(
{
url: dispatchWebAPIEndpointURL + 'json/' + Method,
type: 'POST',
data: Data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSucces,
failure: OnError
});
}
function Authentification() {
var AuthentificationRequest = new Object();
AuthentificationRequest.Credential = new Object();
AuthentificationRequest.Credential.License = "license";
AuthentificationRequest.Credential.Login = "login";
AuthentificationRequest.Credential.Password = "password";
AuthentificationRequest.Credential.EMail = null;
AuthentificationRequest.Credential.Language = 'fr-FR';
APICallPOST('Authentify', JSON.stringify(AuthentificationRequest), function (response) {
if (response.Status == 200 && response.Authentified == true) {
alert('Authentification success');
}
else {
alert(response.Errors[0]);
}
},
function (response) {
alert('Error');
});
}
使用RestClient在Ruby中的函数
url = 'http://dispatchweb.eureka-technology.fr/webmanager/WCFDispatchAPI.svc/REST/';
credential = {
"Credential" => {
"License" => "License",
"Login" => "Login",
"Password" => "Password",
"EMail" => nil,
"Language" => "en-EN"
}
}
request_url = url + "json/Authentify"
headers = {
contentType: "application/json",
dataType: "json"
}
RestClient.post(request_url, credential.to_json, headers) {|response, request, result, &block|
case response.code
when 200
p "It worked !"
response
when 423
raise SomeCustomExceptionIfYouWant
else
p "you messed up"
response.return!(request, result, &block)
end
}
javascript函数工作正常,我得到了成功的回应。 ruby版本一直给我500错误和消息
"在WCFDispatchWebAPIErrorHandler& #xD; \ n方法: DeserializeRequest& #xD; \ nMessage:传入的消息有一个 意想不到的格式' Raw'。 op \ XC3的预期消息格式 \ xA9ration是' XML&#39 ;; '的Json'"
我想知道我是否真的以正确的方式传递了凭证。
任何想法都将不胜感激!谢谢!
解决 事实证明
headers = {
contentType: "application/json",
dataType: "json"
}
应该是
headers = {
content_type: "application/json",
data_type: "json"
}
答案 0 :(得分:0)
你在哪里:
headers = {
contentType: "application/json",
dataType: "json"
}
应该是
headers = {
content_type: "application/json",
data_type: "json"
}