I've been programming in Objective-C for years, but I need to work with Parse Cloud Code and I'm new to Javascript.
Here's my code:
Parse.Cloud.define("test", function(request, response) {
return Parse.Cloud.httpRequest({
url: 'https://api-ilv.trulioo.com/partner/v3/truDetect',
params: {
'Content-Type' : 'application/json',
'api_key':'1234567890',
'provider_name':'fb',
'provider_url':'https://www.facebook.com/testing'
}
}).then(function(httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse.text);
},
function (httpResponse, error) {
console.error('Request failed with response: ' + httpResponse.text);
response.error('Request failed with response: ' + httpResponse.text)
});
});
What could I be doing wrong?
EDIT
Made some changes to log in the code above. Here's the response:
{"code":141,"error":"Request failed with response: 405: Method Not Allowed"}
So now, it looks like the method is returning the error correctly. However, what does error code 405 mean?
It was POST. Needed to change it to POST
Parse.Cloud.define("test", function(request, response) {
var baseurl = "https://api-ilv.trulioo.com/partner/v3/truDetect";
var params = {
'api_key':'1234567890',
'provider_name':'fb',
'provider_url':'https://www.facebook.com/testing'
}
return Parse.Cloud.httpRequest({
method: 'POST',
url: baseurl,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: params
}).then(function(httpResponse) {
response.success(httpResponse.text);
},
function (error) {
console.error('Console Log response: ' + error.text);
response.error('Request failed with response ' + error.text)
});
});
The then
part of the Parse.Cloud.httpRequest
expects two functions - the first one in for the success case and the second one for the error case.
Parse.Cloud.define("test", function(request, response) {
return Parse.Cloud.httpRequest({
url: 'https://api-ilv.trulioo.com/v3/truDetect',
params: {
'api_key':'1234567890',
'provider_name':'fb',
'provider_url':'https://www.facebook.com/testing'
}
}).then(function(httpResponse) {
response.success(httpResponse.text);
},
function (error) {
response.error("Error: " + error.code + " " + error.message);
});
});
My guess is your request to api-ilv.trulioo.com
runs into a problem, but since your code never calls response.error
you see the "success/error was not called" error.
For single request http bridge you don't need .then functions : You can use it more simply with success / error function :
Parse.Cloud.define("test", function (request, response) {
var baseurl = "https://api-ilv.trulioo.com/v3/truDetect";
Parse.Cloud.httpRequest({ url: baseurl,{
'api_key':'1234567890',
'provider_name':'fb',
'provider_url':'https://www.facebook.com/testing'
},
success: function (httpResponse) {
response.success(httpResponse.text );
},
error: function (httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.