How we can set the response header to Access-Control-Allow-Origin: * in jQuery Ajax
Is there any way to set the response header to ("Access-Control-Allow-Origin","*") in jQuery Ajax request, as I am not sure how to do that.. this is my below code.. Any suggestions will be appreciated..
var on_show_info = function() {
request_info = $.ajax({
url: search_metadata + current_doc_info.id,
type: 'GET',
async: true,
success: on_metadata,
error: on_metadata_error
});
};
var on_get_metadata= function(data, text_status, XMLHttpRequest) {
alert("data");
console.log(data);
var html = "";
var info = {};
html = html + "<table border ='0'>";
html= html + "<tr>";
html =html + "<td><span style='color:#8A8A8A'>Item Location:</span> " +(data.response.docs[0].itemLocation)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>Cycle: </span>" +(data.response.docs[0].lifeCycle)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>Revision: </span>" +(data.response.docs[0].revision)+"</td></tr>";
html =html + "<tr><td><span开发者_JS百科 style='color:#8A8A8A'>Timestamp: </span>" +(data.response.docs[0].timestamp)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>ID: </span>" +(data.response.docs[0].id)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>Attachment: </span>" +(data.response.docs[0].hasAttachment)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>Description: </span>" +(data.response.docs[0].description)+"</td></tr>";
html =html + "<tr><td><span style='color:#8A8A8A'>Change: </span>" +(data.response.docs[0].changeNumber)+"</td></tr>";
if(data.response.docs[0].distributionLevel==undefined)
{
html =html + "<tr><td><span style='color:#8A8A8A'>Level: </span>Data Not Available</td></tr>";
}
else
{
html =html + "<tr><td><span style='color:#8A8A8A'>Distribution Level: </span>" +(data.response.docs[0].distributionLevel)+"</td></tr>";
}
//html =html + "<tr><td><span style='color:#8A8A8A'>Renewal Due Date: </span>" +(data.response.docs[0].c_renewal_due_date)+"</td></tr>";
if(data.response.docs[0].asicBomSubdescriptor==undefined)
{
html =html + "<tr><td><span style='color:#8A8A8A'>descriptor: </span>Data Not Available</td></tr>";
}
else
{
html =html + "<tr><td><span style='color:#8A8A8A'>descriptor: </span>" +(data.response.docs[0].asicBomSubdescriptor)+"</td></tr>";
}
html =html + "<tr><td><span style='color:#8A8A8A'>LifeCycle: </span>" +(data.response.docs[0].lifeCycle)+"</td></tr>";
html =html + "</table>";
$('#popup-meta').removeClass('waiting');
$('#popup-meta .popup-content').html(html);
};
var on_get_metadata_error = function(XMLHttpRequest, text_status, error) {
alert("error");
$('#popup-meta').removeClass('waiting');
$('#popup-meta .popup-content').html('<span class="data-error">Unable to get meta data.</span>');
};
You can’t change the response header from client code, it comes from the server.
(You can of course change your request headers (see the jQuery docs for ajax
), but that's not going to help here).
If you also have control over what your server sends, then by all means update your post accordingly (i.e. give details of platform / language etc.)
"Server response header" states for Server response header, not Client request header so YOU CAN'T CHANGE RESPONSE HEADER. You have to put it at the top of file you're retreiving from.
<?php
header("Access-Control-Allow-Origin: *");
Those headers are for organization websites to communicate with each other. In other words, you have to control all the servers involved. Providing those headers simply relaxes the same-domain restriction for sites with different domain names all owned by the same individual or organization. It does not provide a mechanism for subverting same-domain restrictions with a 3rd-party.
精彩评论