0

Where should i add this function response.addHeader("Access-Control-Allow-Origin", "*"); to avoid the error "No 'Access-Control-Allow-Origin' header is present on the requested resource".

This is my ajax function.Now I can I get ajax response only from www.myspansalon.com..I want response from myspansalon.com.

<script>
function getdetails(rate){

var itemid=jQuery("#itemid").val();
var userid=jQuery("#userid").val();
jQuery("#clear").show();
jQuery("#uservote").text('loading...');
jQuery.ajax({
    type: "POST",
    url:"http://www.myspansalon.com/joomla/index.php?option=com_flexicontent&format=raw&task=myvoting",
       
    data: {item_id:itemid, user_id:userid, rating:rate }
    }).done(function(result){
     
 jQuery("#uservote").text(result);
    });
}
</script>
Barmar
  • 741,623
  • 53
  • 500
  • 612
izaan
  • 11
  • 2
  • 7
  • possible duplicate of http://stackoverflow.com/questions/7686827/how-can-i-add-a-custom-http-header-to-ajax-request-with-js-or-jquery – Avinash Babu Nov 02 '14 at 05:51
  • The server sends response headers, not the client. – Barmar Nov 02 '14 at 05:52
  • You can't get around the same-origin policy in the client. If you can't change the server to send that header, you need to use a proxy in your own domain. – Barmar Nov 02 '14 at 05:57

2 Answers2

0

I think you may try xhr.setResponseHeader("Access-Control-Allow-Origin", "*");, please check your modified code below. I'm not sure whether it will work, because basically the response header is set in the server not in the client.

<script >
function getdetails(rate) {

    var itemid = jQuery("#itemid").val();
    var userid = jQuery("#userid").val();
    jQuery("#clear").show();
    jQuery("#uservote").text('loading...');
    jQuery.ajax({
        type: "POST",
        url: "http://www.myspansalon.com/joomla/index.php?option=com_flexicontent&format=raw&task=myvoting",

        data: {
            item_id: itemid,
            user_id: userid,
            rating: rate
        },
        success: function (response, status, xhr) {
            xhr.setResponseHeader("Access-Control-Allow-Origin", "*");
            if (status == "success") {
                alert(response);
            } else {
                alert(status + ' - ' + xhr.status);
            }
        }
    }).done(function (result) {

        jQuery("#uservote").text(result);
    });
} < /script>

To set response header in the server side :-

If the webservice website is developed in java and it's using tomcat as the server then you can refer this post. In a java web application you can do it with javax.servlet.Filter class implementation (independent of the server). But, having seen the url used in the code you posted, it seems to be php, so you may follow this link.

Community
  • 1
  • 1
Visruth
  • 3,430
  • 35
  • 48
0

First you are trying to access another domain by AJAX so SOP (Same-origin policy) should be enable on another domain.

you can add header in beforeSend event, see below

beforeSend: function(xhr) {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
    }
Girish
  • 11,907
  • 3
  • 34
  • 51
  • This won't work. This is a response header, not a request header. It has to be sent by the server. – Barmar Nov 02 '14 at 05:56
  • right @Barmar but he will need to enable (Same-origin policy) on server to access domain.. – Girish Nov 02 '14 at 05:59