Wednesday, 25 September 2013

Error while calling Cross domain API

Error while calling Cross domain API

I am calling a cross domain webservice api from ajax in my js, but i am
getting an error as:-
"XMLHttpRequest cannot load http://url2.com/social/polling/get_poll.
Origin http://url1.com is not allowed by Access-Control-Allow-Origin"
i also tried to set Access-Control-Allow-Origin to * in header of request
, but then to no success, i am getting same error
Below is what i am actually doing:-
$.ajax({
type : "POST",
dataType : "jsonp",
data : {
pollId : pollId
},
/* header : {'Access-Control-Allow-Origin':'*'}, */
url : "http://url2.com/social/rs/polling/get_poll",
beforeSend : function(xhr) {
xhr.setRequestHeader('Access-Control-Allow-Origin',
'*');
},
success : function(response) {
var html;
var html = "<div ><h2>" + response.topic + "</h2>";
for ( var index = 0; index < response.options.length; index++) {
html = html
+ "<input type=\"checkbox\" name=\"option\" value=\""
+ response.options[index] + "\" />"
+ response.options[index] + "<br/>";
}
html = html
+ "<input type=\"button\" value=\"Submit\"
onclick=\"pollIT("
+ response.pollId + ", '" + response.topic
+ "'); \" /></div>";
$("#question").append("");
$("#question").append(html);
html = "";
},
error : function(e) {
console.log(e);
return false;
}
});
I have also tried setting header as header :
{'Access-Control-Allow-Origin':'*'}, but still unsuccessful. Please Help
me to get out of this

No comments:

Post a Comment