Ajax request with Jquery

Asked

Viewed 5,038 times

1

Good morning,

In my work, a biometric reader system was implemented to control the output and entry of personnel. Such a system has an API, which gives access to your data. Every API is in Ajax with Jquery. I looked at W3schools on the site https://www.w3schools.com/jquery/ajax_ajax.asp and learned how to do it. This is the API code:

$.ajax({
    url: "/login.fcgi",
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        login: 'admin',
        password: 'admin'
    }),
    success: function(data) {
        session = data.session;
    }
});

So I made the following HTML:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $.ajax({
                        url: "http://10.3.48.199/login.fcgi",
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify({
                            login: 'admin',
                            password: 'admin'
                        }),
                        success: function(data) {
                            session = data.session;
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <button>Teste</button>
    </body>
</html>

However, when I click the button, it, on the console, gives me errors of CORS. I searched the internet and added the following line below the url line: "Access-Control-Allow-Origin: *,". This removed the error of CORS. But, I cannot access the server. No error appears. No message appears either. I have tried to give a message alert to see if the session would appear, but also nothing. Can anyone help me? Maybe it’s a code semantic error.

  • What returns in variable data, inside success? Put a console.log(data) and see what returns in the browser console.

  • The API does not have its own authentication system or is already used?

  • use Chrome dev tools and look at the "network" tab to see how the request is behaving.

  • Try removing the contenttype.

  • I used the.log(data) console and nothing appears in the Firefox browser and nothing appears.

  • The API has its own authentication system, but I’m accessing the manufacturer’s API manual to create our own personal input and output control system, taking only the database from the biometric reader.

  • I removed the content type but saw no difference.

  • Unfortunately here in the company is not allowed to use Chrome :(

  • I’m wondering if this <pre>Access-Control-Allow-Origin: *,</pre> is right.

  • If I access the link http://10.3.48.199/login.fcgi, it generates me the following in the browser: {"error":"Invalid data","code":1}

  • I tested the console.log(...) in firefox and did not work.

  • @Luizdamata, put your contacts in the profile of Stackoverflow. I wanted to talk to you.

Show 7 more comments
No answers

Browser other questions tagged

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