jsonp parse to XML - ajax

I'm having the following problem.
I'm developing an app with Phonegap, and I have some trouble with the AJAX call on Blackberry.
When I call the ajax service with datatype = 'xml', it works fine on Windows Phone, and Android but failed on Blackberry.
This is the code:
jQuery.ajax({
url: requestedUrl,
dataType: 'xml',
timeout: this.timeout,
beforeSend: _.bind(this.beforeSendCallback, this),
success: _.bind(this.internalSuccessCallback, this),
error: _.bind(this.internalErrorCallback, this),
headers: {'X-Requested-With': 'XMLHttpRequest'}
})
I read on others post, that you cannot do anXMLHttpRequest to an external domain, which is the error that Blackberry shows (but I don't understand why it works on the other two platforms.) So I try to change the dataType to 'jsonp', but a syntaxError shows up. I tried with jsonp text xml, but for some reason it does not and returns the same
Uncaught SyntaxError: Unexpected token <
Looking, the response of the WS, it gave me back a valid XML. So the question is,
How can I use that XML if I'm making the call with jsonp?
EDIT:
Actually, the problem was that the "origin" header was "local:\" (my app), so BB was blocking the content of the response.
The solution was to add that domain to my config.xml, like this:
<access uri="local:///" subdomains="true" />
Unlikely Android and Windows Phone, in the config.xml you have to set access to the domains with uri, not origin and the * wildcard is not allowed with XmlHttpRequest (XHR) on BlackBerry 10.

Related

Cannot Make AJAX Call in Google Ripple PhoneGap Emulation (500 Error)

I'd add a comment here:
PhoneGap application not working on Google Ripple
but given my low reputation on StackOverflow (as elsewhere), I can't. That thread raises similar issues but does not answer my question. I am trying to test the functionality of an HTML5 page that will eventually be made into a mobile app with PhoneGap. The page makes an AJAX call to a JSON service via jQuery:
$(document).ready(function() {
$.ajax({
url: 'latest.json',
type: 'get',
datatype: 'json',
processData: false,
success: function(data) {
//…make it so
});
});
and runs flawlessly as HTML5 in Chrome. However, when using the Ripple PhoneGap emulation available for Chrome, the JSON fails with a 500 error:
GET https://rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=latest.json 500 (Internal Server Error) rippleapi.herokuapp.com/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=latest.json:1
The suggested answer to the question linked above reads:
I had the same issue. It was happening when I was trying to connect to my WebAPI service hosted on IISExpress.
After I changed hosting to my local IIS server, the error dissapeared (sic) and I was able to connect to my WebAPI service using Ripple.
But I'm not running IIS or indeed anything locally -- it's all running off a remote server hosted by an ISP. Since, as I say, this page runs fine in non-emulation mode, the fault would appear to be in Ripple. Any help making this emulation operate correctly will be greatly appreciated.
$.ajax({
type: "GET",
url: serviceurl + "/GetBusinessPartner/",
dataType: "json",
crossDomain: true,
success: function (responseData) {
},
error: function (xhr) {
}
});
This worked for me and in ripple setting disable cross domain proxy.

Absolute/relative address in ajax call in jquerymobile

The following code works fine in iPads and iPhone (4,5) in Safari and Chrome. In contrast, the ajax call won't work (runs straight to the onError function) in Android devices and desktop browsers.
When I exchange the absolute URL for a relative one, the success/failure outcomes are reversed in these two groups.
How do I get around this problem (I'm running jquerymobile 1.3.0 beta)? Thanks/Bruce
$(document).ready(function() {
$("#submit").click(function(){
var formData = $("#loginf").serialize();
$.ajax({
type: "POST",
url: "http://mydomain.org/m2/scripts/site/bpg_process.asp?id=lg",
cache: false,
data: formData,
dataType: 'json',
success: onSuccess,
error: onError
});
return false;
});
});
Do you know that cross-domain Ajax calls are not allowed ?
Your problem may be linked to the URL from which you're making the request, and not to the browser you're testing in.
Read this for more details : http://en.wikipedia.org/wiki/Same_origin_policy
The important thing here for you is that the policy excludes different subdomains.
Ex. if you're sending a request from http://www.mydomain.org to http://mydomain.org it will fail, and vice versa.
What i do in your case usually is use the complete URL "/m2/scripts/site/bpg_process.asp?id=lg" without the protocol and host but with the starting "/" so it can be referenced from anywhere in the URL tree.
What is the "relative URL" that you're using and that "doesn't work"?
Like darma has mentioned. "cross-domain Ajax calls are not allowed".
Use absolute "local" pathing instead.
a slash '/' at the beginning of a link referees to the document root.
if you need to refer to an external domain, use ajax to call a local .asp document that calls the external page for you and returns the data you want in json.
Im not sure what this is in asp but in php, curl works.

jQuery ajax POST from local file to access a cross domain not working

As the title says, I'm trying to access (POST) using jQuery AJAX call to a web url, http://host:port/... or http://localhost:8080/... from a local HTML file, c:\home.html. I can't get it to work.
I did Google and also saw several questions here but I can't get it to work. I need some help here. Here is what I've tried so far.
dataType: jsonp
crossDomain: true
Setting the header in my response:
response.setHeader("Access-Control-Allow-Origin", "*");
None of the three browsers are working - IE, FF or Chrome. The request is never reaching the server. Here are some of the errors I'm seeing.
No Transport (IE) if not jsonp is used.
NS_BINDING_ABORTED / Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED) in FF
This is my code. I would appreciate any help. I'm using jquery-1.8.2.min.js.
var http_host = "http://localhost:8080";
function su (pc, p) {
var suUrl = http_host + "/ps/api/v2/authorize.json";
$.ajax({
type: 'POST',
url: suUrl,
data: {
phone_cell: pc,
password: p,
},
dataType: "json",
crossDomain: true,
success: osu,
error: oe
});
return false;
}
function osu (d) {
console.log(d);
}
function oe(xhr, ts, et) {
alert("ServerError: " + et);
}
An example would be a perfect pointer.
I suppose my code got messed up w/ all the different solutions that I was trying. I was finally able to get it to work w/ setting the header (solution that was recommended and worked for others). All that I had to do to get it to work is add the following to my REST service response.
response.setHeader("Access-Control-Allow-Origin", "*");
Update:
I thought I figured this out but I've not. There is more it than just setting the header. Anyways, in my specific situation. I was trying to run my app (html, js) off of the hard drive specifically on chrome and trying to access web services available on the cloud.
Here is how I finally solved the problem. I started the chrome w/ the following parameters.
--disable-web-security -–allow-file-access-from-files
Like I mentioned earlier, this app is really a desktop application that will be run as part of the chromium embedded framework.
Thanks every one for your input.
You can't make a cross-domain request from a local file because it's not on a domain. You need to host C:\home.html on a local webserver instance in order for it to work.

$.ajax with JSONP causes IE8 to throw security warning

I have a https site. I'm running LifeRay on Tomcat. I'm using the following URL:
http://gdata.youtube.com/feeds/api/videos/ID?v=2&alt=jsonc
and
jQuery.ajax({
url: URL,
dataType: 'jsonp',
async: false,
success: function (obj) {
processData(obj);
}
});
to get the data and then processing it. It works on all the browsers. The only problem is that I get a security warning in IE8.
Question 1: Is there any way to get the JSON data securely and processing the data without IE throwing any warning messages?
Question 2: How and where can I set this: Access-Control-Allow-Origin: http://youtube.com, so that maybe IE won't throw any warning messages?
Try using
https://gdata.youtube.com/feeds/api/videos/ID?v=2&alt=jsonc
Since you are in https, IE wants all resources accessible within that domain to be secured. I feel previously you were using http:// in the request URL of youtube. Change it https, it may solve the security warning issue.
If a secure page loads any nonsecure resource, its going to throw the warning. The only way to get around it is to load everything from https.
Here you are trying to load a non secured resource (http://gdata.youtube....) in the secured website.
HTH

AJAX/JSONP Question. Access id denied using IE while requesting corss domain

Ok, Here we go. I have already searched the Stack for the answer i have found some useful info but i want to clear up some more things. I also search the net for the answer but no real help.
I have worked with some api (yelp, ouside.in). In yelp i use to inject the script to head with the url request to the api with a callback funcion. I worked fine in all browsers. But while using outside.in api when i call the url the callback in not working.
In yelp they have a url field can be used like that callback=callbackfuncion so the callback will automatically called.
But in outside.in there is not such field available. Is there are any standard command for callback function which will work regardless of any server/api?
I also tried a standard ajax request using jQuery $.ajax() function. It worked for my local pc for both IE and other browser but did not working in IE showing the error: access denied, other borwser seems ok. Firebug in my FF also don't notice any errors.
Outside.in has an javascript example but it is too hard to me to understand
github.com/outsidein/api-examples/tree/master/javascript/browser/
site i am working: http://citystir.com
yelp: yelp.com
outside.in: outside.in
Techniqual info:
i am using: wampserver in local, wordpress for hosting, Godaddy, apache for remote with linux.
Codes:
Using Jquery $.ajax
url is like: "http://hyperlocal-api.outside.in/v1.1/states/Illinois/cities/chicago/stories?dev_key="+key+"&sig="+signeture+"&limit=3
function makeOutsideRequest(url){
$.ajax({
url: url, dataType: 'json', type: 'GET',
success: function (data, status, xhr) {
if (data == null) {
alert("An error occurred connecting to " + url +
". Please ensure that the server is running and configured to allow cross-origin requests.");
}else{
printHomeNews(data);
}
},
error: function (xhr, status, error) {
alert("An error occurred - check the server log for a stack trace.");
}
});
}
Thanks!
This question was asked in the Outside.in developer forums this morning as well (presumably by the same person). Here's a link to that discussion: http://developers.outside.in/forum/read/97053
To summarize, the Outside.in API does not support JSONP, but CORS support is included in the next release of the API, which will hit in the near future.

Resources