Consume API with Jquery and save to database

Asked

Viewed 666 times

0

Good night! I am trying to consume an API using Jquery and Laravel. I created the route to access and save the data, access always returns me with status 0.

web php.:

Route::any('http://moviecom.com.br/MoviecomAPI/','MoviecomController@store')->name('movieAPI');

In the view I put the javascript code:

$.ajax({
                headers: {
                    'user_token': '',
                },
                type: 'GET',
                dataType: "json",
                url: "{{!! URL::to('http://moviecom.com.br/MoviecomAPI/') !!}}",
                data: {
                    'praca': 'JU2',
                    'data_ini': '2018-05-10',
                    'data_fim': '2018-05-20'
                },
                success: function(response) {
                    console.log(response);

                    if(response.status == "Success") {
                        $(response.data[0].filmes).each( function(i, el) {

                            // Pegar os dados e salvar no banco
                        })
                    }
                    else {
                        console.log(response.data[0]);
                    }   
                }

I am using the token correctly and the api documentation says nothing about.

  • Try to add in the AJAX request contentType: "application/json" to indicate that the data being sent to the server is of the JSON type and use the function data: JSON.stringify(seusDados) to convert the data to a JSON string.

  • Correction : use contentType: "application/json" to indicate that the data being received by the server response are of type JSON.

  • I tried to add the contenttype, but still keeps returning me this answer: [Object Object]{readyState: 0, responseJSON: Undefined, status: 0, statusText: "Error: Aces..."}

  • What is the documentation link for this Api? Your route is wrong too is not how you do it ... !!!

  • @Virgilionovic the documentation is http://moviecom.com.br/MoviecomAPI/documentation/ I am trying some other possible solutions as well, but without success.

  • Where you created your token?

  • @Virgilionovic the token sent me and I have tested in code without the Aravel, rotate the data according to the documentation, but with the Laravel does not work. Returning only status: 0

  • Because the Laravel was done wrong.

Show 3 more comments

1 answer

0

Good afternoon, friend. If I understand correctly, what you want is to receive the data from Moviecom on the frontend and play for a route in the backend. If so, first of all, you created a route in the Routes to the URI of the Moviecom API, when it should actually be a route to your domain. The right thing to do:

Route::post('<<rota dentro do seu sistema>>', 'MoviecomController@store')

And inside the controller, create a logic to take the data from the frontend with request()->all() and play for the database.

About your AJAX, pass the url as follows::

url: 'https://moviedata.com.br/MoviecomAPI/'

Other than that, you would need to create another POST-type AJAX to play the information received from Moviecom for the route you have placed on Route::post().

But since you are already saving the API data in the database, why not just pull the API data in the backend (using Curl or file_get_contents)?

Browser other questions tagged

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