Lindsay February 2016

How can I execute this cURL command with AJAX?

I've searched up and down and have tried a variety of different AJAX implementations to try and figure out how to go about doing this.

I have a pretty simple cURL command, that should login and authenticate with the API:

curl -c cjar -b cjar -k -H "Content-Type: application/json" -X POST https://bmc/login -d "{\"data\": [ \"username\", \"password\" ] }"

How am I supposed to write this in AJAX? :[

One issue: Failed to load resource: net::ERR_INSECURE_RESPONSE

The request is currently stalled... so any idea on how to go about writing this via AJAX would be extremely helpful.

Edit: Just wanted to make a note that I am particularly unsure of how to handle -c cjar -b cjar and especially -k

Edit: The net::ERR_INSECURE_RESPONSE has to do with a self-signed certificate issue, at least I'm pretty sure that's the problem. I've added custom "trust" settings to the localhost certificate, but I'm still have issues.

enter image description here

Thanks

Answers


Oliver Queen February 2016

cURL is able to send and receive data cross domain to remote servers but that is not the case with javascript (for security reasons).

I would suggest writing a server-side proxy that gathers the information you need then displays it in a way that you can use AJAX to then get from the page.

A curl example in PHP:

<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);     
?>

Post Status

Asked in February 2016
Viewed 3,211 times
Voted 11
Answered 1 times

Search




Leave an answer