简体   繁体   English

解析云代码httprequest错误

[英]Parse Cloud Code httprequest error

I've been programming in Objective-C for years, but I need to work with Parse Cloud Code and I'm new to Javascript. 我从事Objective-C编程已有多年了,但是我需要使用Parse Cloud Code,并且对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? 但是,错误代码405是什么意思?

It was POST. 那是POST。 Needed to change it to POST 需要将其更改为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. then的一部分Parse.Cloud.httpRequest预计两个函数-第一个用于在成功的情况下,第二个为错误的情况下。

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. 我的猜测是您对api-ilv.trulioo.com的请求api-ilv.trulioo.com了问题,但是由于您的代码从不调用response.error您会看到“未调用成功/错误”错误。

For single request http bridge you don't need .then functions : You can use it more simply with success / error function : 对于单个请求的HTTP网桥,您不需要.then函数:您可以通过成功/错误功能更简单地使用它:

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);
        }
    });  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM