XMLHTTPRequest not working on browsers except IE - xmlhttprequest

I am trying to get some data using the WCF Rest based service.
This is my code.
jQuery.support.cors = true;
$.ajax({
url: http://localhost:2545/Service/GetData,
data: JSON.stringify(temp),
beforeSend: function (xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); },
type: "POST",
contentType: "application/json charset=utf-8",
dataType: "Json",
crossdomain: true,
success: function (result) { ProximitySucceeded(result) },
error: function (result) { debugger; ServiceFailed(result) }
});
My website is running on http://localhost:1600 and service is on http://localhost:2545.
Its working on IE fine. But on chrome/ firefox /safari returning this error
"Origin http://localhost:1600 is not allowed by Access-Control-Allow-Origin."
Please help as the service is not being accessed from any browser other than IE.
Thanks.
Mohit.

i think this may be counting as cross site scripting and being denied because the different port makes it a different domain.
to get round this you may need to setup a proxy.
hope that helps

Related

Crossdomain jQuery ajax request was not called and give error status "parsererror"

I am trying to call cross domain url. which has response text as below.and it is valid json response.
[{"LANG_CODE":"UK_EN","COU_ISO_CODE":"BGR"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"HUN"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"PRT"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"UGA"}]
Jquery ajax code which i am using for calling cross.
$.ajax({
url: "http://someDomainName/restfulservice/Api/Countries/Get_Json",
dataType: 'jsonp',
crossDomain: true,
async: false,
success: function (data) {
alert("success >> "+data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error : "+errorThrown + ", textStatus >> "+textStatus);
}
});
every time it goes to error block. when i inspect this service in browser then it gives response text with valid json string.but through code i am getting error "jQuery18305917718204907095_1409810925309 was not called, status: parsererror".
while this code is working for the url "http://api.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9&username=demo".
what could be the issue for same ?
It is sending back JSON but you have said dataType: 'jsonp'. JSON is not JSONP.
Either change the service to support JSONP or change the client to expect JSON (which might require you to find some other way to circumvent the same origin policy)

CORS with Sencha Touch 2

I'm developing an application which need CORS, I did some google but could not understand that. My application is running on localhost:8000 while I'm making a ajax request to localhost:9090. Below is my Ajax request code...
Ext.Ajax.request({
url : 'http://localhost:9090/by-api',
method: 'POST',
jsonData : api_no,
disableCaching: false,
withCredentials: true,
useDefaultXhrHeader: false,
headers: {
"Content-Type":"text/plain",
'Access-Control-Allow-Orgin':'*',
'Access-Control-Headers': 'x-requested-with'
},
success: function(response){
console.log(response.responseText)
},
failure: function(response){
console.log(response.responseText)
}
});
Also I want to do a Basic Authentication so I tried putting 'Authentication' in headers but nothing is working..
The directive
'Access-Control-Allow-Orgin':'*'
must be included server side in the response header, not in the client request.
Follow this document to reference W3-CORS

Callback not getting called on Internet Explorer for Ajax JSONP call

I'm making an Ajax call using JSONP to fetch JSON data from a third-party (domain different from my site) and its working on Chrome and Firefox but failing on IE (9 & 10 are the versions I have tried). In the IE debugger I see the call is completing (with Http 200) but the callback I've specified for the JSONP call is not being invoked.
My code looks like this:
$.ajax({
url: 'http://api.yipit.com/v1/deals/?key=mykey&division=seattle&limit=1000',
dataType: 'jsonp',
contentType: "text/javascript",
async: false,
cache: false,
crossDomain: true,
jsonp: 'callback',
jsonpCallback: 'processResult',
error: function (xhr, textStatus, error) {
window.alert(error);
},
success: function (data) {
window.alert(JSON.stringify(data));
_yipit_deals = data.response.deals;
}
});
And the response body looks like this:
<html><body><pre>processResult({
... [valid JSON data]
});</pre></body></html>
When the call is made, the error function is getting invoked with error as: Error: processResult was not called and the IE debugger shows a script error caused when trying to parse the <html><body><pre> tags in the response. When running on Chrome and Firefox these html tags don't exist in the response body and I'm not sure why the response is different in the case of IE.
It appears that these tags are causing IE to barf and not be able to invoke the callback. I've tried specifying other values for contentType such as "text/html", "text", "application/javascript" and even not specifying it at all, but its not made any difference. I'm using JSONP to get around the cross-domain issue.
Any ideas? Thanks!
You have to pass an explicit format argument to the URL:
&format=json
Otherwise, it's just pretty-printing the output:
<html><body><pre>{
"meta": {
"code": 401,
"message": "Key not recognized",
"name": "AuthenticationFailed"
},
"response": {}
}</pre></body></html>
Also, I would set async back to true.

WCF Rest Service Call

I am calling a rest service in ajax.
If I add the svc file in the same project and call I am able to call the url
The below is the URL working
http://localhost:1947/GreenViewService.svc/?callback
If I add the svc in separate project layer and call in ajax I am unable to call it says 403 forbidden error and sometimes 405 error.
If I call the below url from different layer not working is it because port no. varies. should I give any access rights.
function GetDataPoints() {
alert('S');
var action = 'http://localhost:1984/GreenViewService.svc/?callback=';
alert(action);
$.ajax({
"type": "GET",
"url": action,
"cache": false,
"contentType": "application/json; charset=utf-8",
dataType: "text",
"success": function(result) {
alert(eval(result));
var json = eval(result);
//fillDropDown(json, prefix + 'DataPoint', 'NodeLabel', 'ID');
}
});
}
As you say it works when in the same project but not in a different project.
The difference is that when it is in a different project it is a cross site call.
To do a cross site call you need to use JSONP

Retrieve JSON Request Payload in WCF Service

I have an IErrorHandler set up for funneling all wcf errors through log4net. I'd like to get the json payload data from the request before logging it to the server, but I can't seen to find it in System.Web.Context.Current.Request. I expected it to be in the InputStream, but that's empty.
I'm currently using jquery to do an AJAX post with the json passed in as data.
$.ajax({
url: 'http://test.com/myservice/service.svc',
data: JSON.stringifyWcf({"id":1, "description":"thing"}),
type: 'POST',
processData: true,
cache: false,
contentType: 'application/json; charset=utf-8',
timeout: 5000,
dataType: 'json',
success: function (result) {
//do stuff
}
});
Where I would like to get the payload {"id":1, "description":"thing"}
I guess that the input stream is empty because it has already been consumed. You'll need to hook into the system before it reads the input stream to save it for later I believe. See Request.InputStream is empty when service call is made
How about: OperationContext.Current.RequestContext.RequestMessage?