I have the following code:
$(function() {
$('#method').change(function() {
var method = $(this).val();
if (method == 'GET' || method == 'DELETE')
$('#json-group').hide();
else
$('#json-group').show();
});
$('#send-button').click(function() {
var url = $('#url').val()
var a = document.createElement('a');
a.href = url;
var method = $('#method').val();
if (method == 'GET')
var data = '';
else
var data = $.trim($('#json').val());
var md5 = data ? CryptoJS.MD5(data).toString(CryptoJS.enc.Base64) : '';
var date = (new Date()).toUTCString();
var parts = [method, md5, date, a.pathname].join('\n');
var hmac = CryptoJS.HmacSHA1(parts, $('#api_secret').val());
var sig = hmac.toString(CryptoJS.enc.Base64);
var auth = 'Zopim-Reseller-API ' + $('#api_token').val() + ':' + sig;
var headers = {'API-Date': date, Authorization: auth};
$.ajax({
url: url,
type: method,
dataType: 'json',
success: function() { alert('hello!'); },
error: function() { alert('boo!'); },
beforeSend: setHeader
});
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', auth);
xhr.setRequestHeader('API-Date', date);
}
});
});
This code should send a GET request, but in Chrome it tells me the following data:
Remote Address: 67.23.229.9: 443
Request URL: link
Request Method: OPTIONS
Status Code: 200 OK
Request Headersview source
Accept: /
Accept-Encoding: gzip, deflate, sdch
Accept-Language: pt-BR, pt; q = 0.8, en-US; q = 0.6, en; q = 0.4
Access-Control-Request-Headers: accept, authorization, api-date
Access-Control-Request-Method: GET
Connection: keep-alive
Host: reseller.zopim.com
Origin: link
And because of this error:
XMLHttpRequest can not load link . Request header field Authorization is not allowed by Access-Control-Allow-Headers.