I've got a simple server app running on Heroku that I'm making ajax calls to from a Cordova app. I've setup CORS on the server app and I can call the server from my browser without any problems. However, when I call the server using the exact same code from within Cordova I get a Bad Request error!!
$.ajax({
url: 'http://{myserver}/{function}',
type: 'GET',
data: { mydata... },
success: function(resp){ alert("success"); },
error: function(err) { alert(err.statusText); }
});
I've tried adding crossDomain: true and dataType: 'jsonp', it makes no difference. I've also checked that all domains are whitelisted in Cordova <access origin="*" />
Why can't I access the server from Cordova when I can access it from my browser?
Figured it out! The reason was that I has a misspelling in data: { ... } so one of the parameters was missing.
Related
I have been using ajax call to fetch data from my remote server on my cordova app/localhost, It just stopped working for no reason, but the same ajax call is still working locally, Im using wampserver to host my site locally,
Below is the code that fails to fetch data but it has been working just fine
$.ajax({
url: 'http://wingup.byethost4.com/trial.php',
type: 'GET',
data: {phone_number:"phone_number"},
success:function(data){
console.log("message was sent");
},error:function(err){
console.log("message was not sent");
}
});
But if i change the url to my localhost it works, Please help me with annoying error
It may have the error like this
CORS header ‘Access-Control-Allow-Origin’ missing
It does not allow remote access. Check this related setting on your server.
i am trying to run my app after using phonegap but failing to pass the AJAX code section
it work perferctly on Ripple emulator but after installing it on my device (android 5.0) the ajax call just failed (Getting the ErrMSG).
Thanks
this is the code i am using:
$.ajax({
type: "POST",
url: "LogIn.aspx/UserCheck",
data: "{'UserName':'" + UserName + "','Password':'" + Password + "'}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
if (msg.d == true) {
window.location.href = "Default.aspx";
}
else
alert("please insert correct username and password");
},
error: function (xhr, textStatus, errorThrown) {
alert('Ajax Call Failed');
}
});
It looks like your ajax call is calling on your local web server. Since you are not running now on your local web server you will need to adjust your ajax call to hit the full url. You have two places (at least in the code you have provided):
url: "LogIn.aspx/UserCheck"
and
window.location.href = "Default.aspx";
This last bit of code really needs to be a page or block of code in your app. This way if the phone is not connected to the Internet for whatever reason you still can give your users a valid error message.
Once this is done you will then need to whitelist your domain in your app.
After deploy to phone, you have a Cross Domain call to your aspx. so you also need to enable Cors.
Why do you redirect to default.aspx after login? if you redirect from the original index.html, which is located in the phone to the default.aspx which is located in the IIS Server, you lost control of the phone application, all plugins will not work, just like your apps become a mobile browser.
btw, r u using vb or c# in the aspx?
I have an ASP.NET WebAPI application running on
http://localhost:13057/worldwind
I have another ASP.NET MVC5 application running on
http://localhost:2425/worldwind
When the client code on the MVC5 application attempts the following AJAX call:
$.ajax(http://localhost:13057/worldwind/images,
{
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: Utilities.Logger.displayAjaxError,
success: this.onImagesRetrieved,
context: this
});
I get error:
XMLHttpRequest cannot load http://localhost:13057/worldwind/api/images. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2425' is therefore not allowed access.
If I change the dataType in the AJAX request to jsonp, the WebAPI is being invoked even though I get error
jQuery18209344927002675831_1412452833242 was not called
How do I get this thing working?
I ended up enabling CORS following instructions from here
This involved 3 steps (well, after fighting with other NuGet upgrade issues):
Adding NuGet package Microsoft.AspNet.WebApi.Cors
Adding the following lines in WebApiCconfig.Register static method:
var cors = new EnableCorsAttribute("http://locahost:2425", "*", "*");
config.EnableCors(cors);
Rebuilding and running.
if your .jsp put in
http://localhost:13057/MySite/JSONP.jsp
use your script section like this:
<script type = "text/javascript" src="http://localhost:13057/MySite/JSONP.jsp?callback=jsonHandler"></script>
hope it will be help.
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.
I have the following script in my web page to call out to a JSON-enabled WCF service that I have created and am hosting on my web server. However, the alert displays "null". When I point to the url in a browser it returns very simple JSON: {"city":"Ann Arbor"}. Also, when I run the page containing the code below with Fiddler running, I can see the service is hit and the JSON returns. But still the success function below returns null. Anyone know what I'm doing wrong? Thanks. -Ned
<script type="text/javascript">
$.ajax({
type: "GET",
url: "http://192.168.192.17:8080/Service.svc/class/",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
</script>
I suspect the problem comes from the fact that you are trying to call a web service using AJAX which is not hosted on the same domain as the calling script and thus you are violating the same origin policy. If you want to be able to call http://192.168.192.17:8080/Service.svc using AJAX, the calling script needs to be hosted also on http://192.168.192.17:8080.
As a possible workaround you could use a server side script acting as bridge hosted on the same domain as the client script or use JSONP if you have control over the web service.
You could define an error function to see what the problem is:
$.ajax({
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown)
},
//.......
})