I have created the User interface in phonegap for android. And now I want to insert data in my web based application which is made using php and mysql. So whenever I click on submit button then the information should be stored on the web database of my web application.
How can I achieve this...?
You also can store your information using session storage and ajax.
formData = {
username: $("#username").val(),
password: $("#password").val()
}
$.ajax({
url: "https://example.com/login.php",
type: "GET",
crossDomain: true,
contentType: 'application/json',
data: formData,
dataType: "json",
success: function(data) {
sessionStorage.setItem("userId", data.user[0].userId);
},
error: function() {
alert("error");
}
});
Related
I am building a mobile app using Cordova and Cloudant. For this I need to make a POST request using ajax. I am using basic authentication with headers useCredentials=true as required by Cloudant. However this gives me 401 on android device. When emulating on browser it gives me a login screen.
Below is my ajax request.
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
accept: "application/json",
xhrFields: {
withCredentials: true,
},
authorization: base64basicAuthString,
crossOrigin: true,
url: http-url,
data: jsonQuery,
success: function(data) {
//process data
},
error: function(jqXHR, status, error) {
console.log("Error getting data. " + jqXHR.status + " " + error);
}
});
I am trying to create a html element by using angularJS. I want to do that because I want to download a file from the client available on the server. I saw that the easy way to do this is by using a html element with the href attribute. Here is my piece of code :
$http({
url: '/process',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: appData.elements
}).then(function sucessCallback(response) {
//Create a link and emulate a click on it
}, function errorCallback(response) {
});
So I send a request to the server. Then, it builds an android project in the purpose of generating an APK. I want to download this APK from the server by clicking on the element.
Create a dynamic <a> tag,
$http({
url: '/process',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: appData.elements
}).then(function sucessCallback(response) {
//Create a link and emulate a click on it
var anchor = angular.element('<a/>');
angular.element(document.body).append(anchor);
//hide with css
anchor.css({display: 'none'});
var body = $document.find('body').eq(0);
body.append(anchor);
anchor.attr({
href: data.content.fileName,
target: '_blank',
download: 'filename.doc'
})[0].click();
anchor.remove();
}, function errorCallback(response) {
});
I develope an App with Phonegap and Jquery Mobile. For some reasons I also need to get data from https domains. On IOS everything works perfect, but on Android I always get the following errors and the requests fails
06-17 17:16:33.890 6079-6154/de.sistecs.einlass E/chromium_net: external/chromium/net/socket/ssl_client_socket_openssl.cc:905:
[0617/171633:ERROR:ssl_client_socket_openssl.cc(905)] handshake failed; returned -1, SSL error code 1, net_error -107
Here is a simple sample code
$(document).on("pageinit", function (event, ui) {
$("#test").click(function () {
$.ajax({
type: "POST",
url: "https://test.sistecs.de/sismedia/test.php",
success: function (response) {
$("#testmsg").text(response);
}
});
});
});
I´ve read hundreds of posts, but didn´t find a solution to my problem.
The certificate of the server https://test.sistecs.de/sismedia/test.php is valide.
Can someone help me?
It seems like a Cross Origin problem.
With jQuery Mobile you should set $.mobile.allowCrossDomainPages and $.support.cors to true
<script>
$( document ).on( "mobileinit", function() {
$.mobile.allowCrossDomainPages = true;
$.support.cors = true;
});
</script>
Also be sure that your PHP page sets the headers accordingly. For example:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE');
header('Access-Control-Allow-Headers: Content-Type');
echo "test";
?>
Edit: I didn't test the code, it is an example. Adapt as you need to.
var myJson = {
'Par1': 'par'
};
$.ajax({
url: baseUrlAjaxServices + "/....",
data: JSON.stringify(myJson),
method: "POST",
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
})
.done(function (data) {
//...
}).fail(function (error) {
console.log(error);
});
I am developing Android application using PhoneGap. I am trying to execute simple login module, it works fine in web browser. But when it comes to apk, its not working. It is not able to post request to Restful API.
var usr = $('#email_address').val();
var pass = $('#password').val();
if(usr != '' && pass != ''){
alert("Before Calling Method");
$.ajax({
url:APIURL +"api/sample/sample123",
type: "POST",
crossDomain : true,
data: {username:usr,password:pass},
dataType: "json",
success: function(data){ /*Here I am posting some dummy code, I can't show original code*/
var response = eval(data);
alert(response.response);
}
});
When I am executing this on android phone, I am getting only first alert above i.e "Before Calling Method" but it is not showing another alert. I have configured res/xml/cordova.xml as
<access uri="*" subdomains="true"/>
Also I have referred some previous related doubts from StackOverflow. But It didn't helped me. Please help me to, I am waiting for positive response...
try this one
formData = {
username: $('#email_address').val(),
password: $('#password').val()
}
$.ajax({
url: APIURL + "api/sample/sample123",
type: "POST",
crossDomain: true,
contentType: 'application/json',
data: formData,
dataType: "json",
success: function(data) {
alert(data);
}
});
I'm trying to develop an application on phonegap on Android.
What I'm trying to do is call a webservice from my application, this will be used for data access.
I found many sheets online on how to do this, and the simplest one I could find that seems straight forward goes like this
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "http://www.webservicex.net/stockquote.asmx/GetQuote",
data: "{'usd'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Insert the returned HTML into the <div>.
$('#Content').html(msg.d);
}
});
});
However, All I can see is the text I had already put into the div, the div contents do not change. I'm testing on a Galaxy SIII device directly, not on a simulator.
My guess is that nothing seems to happen because you are getting a fail, not a success. try adding an error handler and see what happens.
.fail(function() { alert("error"); })
You are likely having 'cross domain' issues (because you are trying to get ajax from a different domain) and may need to use JSONP instead of JSON for your dataType. For more info about JSONP, see this post or the docs
EDIT:
Here is a fiddle of this code in action.
$(document).ready(function() {
//here is one way using the 'error' handler
$.ajax({
type: "POST",
url: "/failOnPurpose",
data: "{'usd'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error:function(jqXHR, textStatus) {
alert("error:" + textStatus);
},
success: function(msg) {
// Insert the returned HTML into the <div>.
$('#Content').html(msg.d);
}
});
//here is another way using the 'fail' request handler
$.ajax({
type: "POST",
url: "/failOnPurpose",
data: "{'usd'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Insert the returned HTML into the <div>.
$('#Content').html(msg.d);
}
}).fail(function(jqXHR, textStatus) {
alert( "fail: " + textStatus );
});
});