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

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)

Related

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.

how to send jsonp request to server in sencha 2.0

I will get this error Repeatedly
Uncaught SyntaxError: Unexpected token : ajax.php
{"error":"Server Error</b>: Method file_get_contents unable to open request body!"}
Ext.data.JsonP.request({
url: 'http://yaksee.com/facebook/ajax.php',
callbackKey: 'callback',
params: {
format: 'json',
},
success: function(result) {
if (result) {
alert('success');
alert(result);
} else {
Ext.Msg.alert('Error', 'There was an error retrieving the weather.');
}
}
});
}},
You Have to disable the Browser's web security
Then it will be ok
Check out the example which works : http://docs.sencha.com/touch/2.1.1/#!/api/Ext.data.JsonP
I changed it to your URL and it stopped working in this fiddle so it seems like issue with webservice or it requires some more data to make request because it is returning HTTP 200 status but with some custom error message

XMLHttpRequest status 0

I'm using jquery ajax call and Chrome javascript console is spitting out an error:
XMLHttpRequest cannot load http://www.1luckypixel.com/eppy/fong_app/index.php/fb_login/login_user. Origin http://1luckypixel.com is not allowed by Access-Control-Allow-Origin.
I've done some searching and find a lot of info for "Origin NULL is not allowed by Access-Control-Allow-Origin." But this is actually giving my domain name as the un allowed origin. I'm not sure what the error means. Also the request goes to the server but doesn't come back and the data in the request isn't past.
Here is my code in case that helps:
$.ajax({
type : 'POST',
url : "<?= base_url(); ?>index.php/fb_login/login_user",
data: {
name:response.name , img:response.link+'/picture' , fb_id:response.id
},
beforeSend : function(thing,data) {
console.log('before', data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
console.log('xmlhttprequest', XMLHttpRequest);
console.log('textStatus', textStatus);
console.log('errorthrown', errorThrown);
}
});
www.1luckypixel.com is not the same as 1luckypixel.com
Use a relative URL in your JavaScript, not an absolute one.
Better yet, pick one of the two hostnames to be canonical and redirect all the traffic from the other one to it (with an HTTP 301 status code).

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?

Adding a social tag to a page

In SharePoint when you click on the "I Like It" icon, it sends json to this URL
"_vti_bin/socialdatainternalservice.json/AddQuickTag"
So I wrote a custom script which sends JSON data
$("a").click(function(){
$.ajax({
type: "POST",
url: "/_vti_bin/socialdatainternalservice.json/AddQuickTag",
data: '{"targetPage":"http://url/calendar.aspx","title":"Documents - All Documents","quickTagId":0}',
contentType: "application/json",
success: function(msg){
alert(msg);
}
});
return false;
});
I get an error which simply says "There was an error processing the request." and the error in the log file says "Request format is unrecognized for URL unexpectedly ending in '/AddQuickTag'."
Is it possible to write a custom script which will post JSON data to this URL and have SharePoint tag a page?
These are the calls that are made for the I Like it functionality
/vti_bin/socialdatainternalservice.json/GetNormalizedPageUrl
Post
{"pageUrl":"http://<web app name>/SitePages/Home.aspx"}
Returned
{"d":"http://<web app name>/"}
/vti_bin/socialdatainternalservice.json/AddQuickTag
Post
{"targetPage":"http://<web app name>/","title":"Home - Home","quickTagId":0}
Returned
{"d":null}
I think you need to do the GetNormalized call first.