M
M
Manuchehr Jalolov2018-12-15 14:55:07
AJAX
Manuchehr Jalolov, 2018-12-15 14:55:07

AJAX, jQuery request with CURLOPT_HTTPHEADER fields like in PHP?

$data = json_encode(["a"=>1,"b"=>true,"c"=>"d"]);
$ch = curl_init("https://$IP:8089/common_api/1.0/$method");
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_TIMEOUT, 3 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
    'Signature: ' . md5($data . $key),
    "Content-Type: application/json"
));
$response = json_decode(curl_exec( $ch ));
$error_code = curl_errno($ch);
curl_close($ch);
return $response;

How to make this request in AJAX?
$.ajax({
url: "https://"+IP+":8089/common_api/1.0/update_driver_info",
dataType: 'json',
contentType: "application/json",
data: {"a":1,"b":true,"c":"d"},
headers: {
'Access-Control-Allow-Origin': '*',
'Signature': 'e664a6438abfee0d10e66f35c4f655c9',// php md5('{"a":1,"b":true,"c":"d"}'.$key)
},
success: function (data, status) {
debugger;
alert("Sucess");
}})

and here is the error:
Access to XMLHttpRequest at 'Access-Control-Allow-Origin' header is present on the requested resource .

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question