Calling API via Post with JS or JQ

Asked

Viewed 825 times

-1

Personal someone could help me by giving an example of how to consume an API that returns a JSON just like this API below:

https://console.faceplusplus.com/documents/6329465

Staff implemented the code below can help me is not returning anything or error.

$('.teste').click(function() {


      $.ajax({
        type:"post",
        url:"https://api-us.faceplusplus.com/facepp/v3/detect",
        data: {
            api_key: 'M...s',
            api_secret: 'y...0',
            url: 'http://emotions.pushsistemas.com.br/web/train/mmi/alegria/1.png'
        },
        success: function(data) {
            console.log("saida",JSON.stringify(data));
        },
        dataType: 'jsonp',
    });

  });
  • The site does not work to give ready codes. You need to try and if you have any problem you turn here. Or you can search for something related that has much already published material.

  • I just wanted an example but thanks anyway.

  • I managed to implement the request could help me ?

  • 1

    Remove the JSON.stringify and do what Rodrigo’s reply says the return will be a JSON from where you can get the data you want.

  • Hi @Sam I removed and made the changes the request happens but still does not appear in the log.

  • but it appears the word "out" that you put?

  • also not @Sam I believe that it did not enter in sucess but in the browser of the 200 and I see the exit as well as Rodrigo

  • 1
Show 3 more comments

1 answer

2


1) According to the documentation in the data to pass a URL of the image the property should be image_url and not url.

2) Change the type of dataType of jsonp for application/json

Final code:

$.ajax({
    type:"post",
    url:"https://api-us.faceplusplus.com/facepp/v3/detect",
    data: {
        api_key: 'MgfOZx5IZCuocbZ5wMmKCdZkd7P0mPhs',
        api_secret: 'ymFeQjguF6lGA8oLlneuAHMtdhq1QSA0',
        image_url: 'http://emotions.pushsistemas.com.br/web/train/mmi/alegria/1.png'
    },
    success: function(data) {
        console.log("saida",JSON.stringify(data));
    },
    dataType: 'application/json',
});`

I did the test and got the following result:

{
    "image_id": "CxX5j9mXrorIFJ/336gFbQ==",
    "request_id": "1543955895,b7599a1a-e67b-4298-9dc5-566c3d13758e",
    "time_used": 1707,
    "faces": [{
        "face_rectangle": {
            "width": 296,
            "top": 316,
            "left": 152,
            "height": 296
        },
        "face_token": "e17f055212b25d1837004bff48d21226"
    }]
}

Evidência

  • Thank you, I am trying Cors error Blocked cross-origin request: Same Origin Policy (Same Origin Policy) prevents reading the remote resource at https://api-us.faceplusplus.com/facepp/v3/detect. (Reason: CORS header 'Access-Control-Allow-Origin' is not present)

  • Guys I got here but I couldn’t get it out in json format on the console.log someone has idea how to do it ?

  • @Adding generators There are settings to be made in the key pane of the API.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.