I am developing an app in phonegap 3.1.0-0.15.0 and I am having problems with an ajax call to a remote server.
I have done all the default stuff, like added Internet acces, with-listed the domain, but the ajax still get's the 404 header.
My ajax call:
$.ajax({
url: 'http://my-host/bus/app-ajax',
type: 'GET',
dataType: "json",
data: 'type=data_out',
cache: false,
crossDomain: true,
processData: false,
contentType: false,
success: function(ret_data) {
alert(ret_data);
},
error: function(xhr, textStatus, errorThrown) {
alert("Ajax error(ajax_helper.get_categorys) xhr.status: " + xhr.status);
}
});
My code has an IP address in the "url" variable of the ajax call, I just changed it here for security reasons.
If I copy the content of my www folder to a website that is on the same host as my PHP respond script(browsers don't allow cross domain ajax calls) than it works. On the device that I am testing if I open a browser I can access the domain, and get the json response, but the app just gives me 404 all the time.
Any ideas on what I can still try to make this work?
I recreated the project with the verbose "phonegap create" command, added the same code, and it works. Was probably some problems with the old project, because I updated phonegap in it a few times.
Related
I'm having a problem to make a call to a rest API.
In the document (FAQ) of the web application there is an example that use AJAX request to make the call. Here an example:
var url = 'https://example.com/yyy';
$.ajax({
type: 'POST',
url: url,
cache: false,
data: {
opt: JSON.stringify(example)
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result)
{
console.log(result);
} });
I created a local page with this code to made the post to the API that is located on a remote server but I receive an error about CORS.
Is there any solution to circumvent this problem? I tried to use firefox plugin to allow CORS but it didn't solve the problem. The session is authenticated via form before use the endpoint.
I see several issues:
Try to run the code from a domain and not from local disk (alternatively you can consider using https://crossorigin.me/ )
How does the authentication work? if with cookies you need to add withCredentials to the ajax request.
Make sure the API returns Access-Control-Allow-Origin: foo header where foo is the domain your code runs in. If you also used withCredentials, you should add Access-Control-Allow-Credentials: true
I am building a web app and for the server I am using nodejs and express. When I make a request to the server just from the browser it works fine, but when I try an ajax request from the client it registers on the server, but gives me an error on the client. Here is what I am using for the ajax request:
$.ajax({
async: false,
dataType: "json",
url: url,
crossDomain: true,
success: function(response) {},
error: function(response){
alert(JSON.stringify(response));
}
});
on server side, when you send the request... set header of response to allow cors:
something like this >>
res.writeHead(200,{
'Access-Control-Allow-Origin' : '*'
});
In place of '*', you can specify a set of urls, you want to server response to...
I wanted to retrieve data using ajax, when open the url in browser it showing data but when i execute this url in ajax code error msg function is running however data is showing in browser.
url: "http://live.nayatel.com/?json=1"
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://live.nayatel.com/?json=1",
cache: false,
success: onSuccess,
error: onError
});
});
its a wordpress site and i used a plugin to retrive its data working in browser but not giving response text.
I have two suggestions, since I am not that sure about your problem.
a) Try changing
success: onSuccess,
to
success: function(data, status) {
onSuccess(data, status);
},
b) If that fails, try adding
crossDomain: true,
I have a simple function that uses $.Ajax. It's working fine when I deploy the this page to my online website, but on my dev environment it doesn't work, but there is no error. I tried both $.Ajax and $.Post. both not working, but in the F12 profiler tool i see the traffic.
Any Idea?
Thanks.
<script >
function RunAjax()
{
alert("before post");
$.post("http://myshulmgr.com/GetData.asmx/GetEventMembers", { PID: "32", EventID: "8" },
function (data) {
alert("Data Loaded: " + data);
});
$.ajax({
type: "POST",
url: "http://myshulmgr.com/GetData.asmx/GetEventMembers",
//data: {PID: iPID, EventID: iEventID},
data: "{'PID': '" + 32
+ "','EventID': '" + 8 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (jsonRes)
{
alert(jsonRes);
},
failure: function (msg)
{
alert(msg);
}
});
}
Look at your browser's error console. There should be an informative message there.
Your dev environment is probably not on the same domain as the endpoint you are calling, so the same-origin policy is preventing your script from retrieving results from it. Since this is supposed to be a dev environment, you should probably set up a copy of the GetData.asmx script in that environment, rather than calling the production endpoints, and reference it as /GetData.asmx (i.e, as a domain-relative path) in your script.
You cannot perform cross-domain request because of same origin policy.
In your console you can see that this is actually the source of your problem:
console says (Aborted), giving a CORS preflight initiatior, i.e you tried to perform an cross-domain request, but it was aborted due to security reasons.
The reason why it works on production and not on dev is because probably your production environment is in the same domain (myshulmgr.com) to which the request is addressed, while dev environment is not.
I would like to call OData .NET web service that authenticates users via basic authentication.
I use following ajax call:
var fullUri = APIUri + "?$format=json";
$.ajax({
url: fullUri,
contentType: "application/json",
dataType: "jsonp",
type: 'GET',
jsonp: '$callback',
beforeSend: function setHeader(xhr) {
xhr.setRequestHeader('Authorization', token);
},
success: callback,
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
},
});
The results are unusable for me:
Calls are blocked because of CORS (until I will paste API url and try to load it in chrome). I tried local html file and html file uploaded to the same domain/port, but authentication fails (according to Chrome console).
Once I enter service URL into chrome address bar, I am asked to provide login name and password by Chrome. If I enter them, they are cached and used even I assign them in beforeSend. How to blocks this behavior?
I've tried a lot of examples how to configure jsonp, headers etc, but did not find working solution yet.
IIS server response header is also configured using "Access-Control-Allow-Origin" value="*".
You can set the HTTP Password and Username in the AJAX Call directly:
$.ajax({
url: fullUri,
contentType: "application/json",
username: <login>,
password: <password>,
...
Use the following to support CORS:
jQuery.support.cors = true;
Regarding the call, are you using HTTPS? Is the certificate valid?