PhoneGap AJAX Login - javascript

I am trying to build a log in application in PhoneGap. Ive been looking around a lot for similar things and I found them plenty... but I do not understand why this pieces of code does not work. When i press the logIn_btn it shows that It cannot reach the "success" piece of code. Thank you for your help!
$('#logIn_btn').click(function(){
user = $('#username').val();
pass = $('#password').val();
var ev = {'papa':true};
if(user!='' && pass!=''){
alert('trying login');
$.ajax({
url: 'https://localhost/server/IF/mobileApp/login.php',
type: 'post',
datatype: 'json',
data: ev,
success: function(data){
alert('asd');
}
});
console.line('done.')
} else {
alert('Please insert your username and password.');
}
});

Your server should not be https://localhost, it should be the name/ip of the server that you'll be logging into.
For additional information, look at your network tab with Chrome, or install a fail handler
$.ajax({
url: 'https://localhost/server/IF/mobileApp/login.php',
type: 'post',
datatype: 'json',
data: ev,
success: function(data){
alert('asd');
}
}).fail(function(jqXHR, textStatus){
});

console.line?I think it would be console.log. Rather try alerting the message. Also make sure the javascript executes after deviceready. Also localhost in android is a very different thing. Try using the ipadress or domain name to access the server.

Related

Execute a function , after form successfully submitted

I'm trying to alter the registration procedure that WordPress is using by adding another 'step.'
Since I added foreign key constraints in my database tables, wp_users need to be filled first and after executing the rest of the code.
My current code for WordPress form submit interruption is:
jQuery("#registerform").submit(function (e){
e.preventDefault();
let self = jQuery(this);
self.unbind().submit();
setTimeout(function(){
if (jQuery("#registerusingwebauthn").is(":checked")) {
let username = jQuery("#user_login").val();
initializeCredentialType({
authenticationMethod: "webauthn"
});
webauthn_registration(username,null)
.then(function(e){
alert(e);})
.catch(function (e){
console.log(e)});
}
},60000);
});
The self.unbind().submit(); part it's used because the wp_users table must be filled in first, or else a violation of constraints will occur.
The webauthn_registration is returning a new Promise and it's defined as follows:
const webauthn_registration=(username,type)=>{
return new Promise((resolve,reject)=>{
// some variable definition here//
if (username === ""){
reject("Error: Username not given");
}
$.ajax({
url: url,
type: 'GET',
data: {'username': username},
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (error){reject(error);},
success: function(serverregdata){
// some variable definition here//
navigator.credentials.create({publicKey: serverregdata})
.then(function(credentials){
// some variable definition here//
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(credentials),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (error){alert(error);reject(error);},
success:function(success){alert(success);resolve('success');}
})
});
}
});
});
}
The problem is that setTimeout() doesn't wait until the promise that webauthn_registration is resolved and reloads the page before webauthn_registration is finished.
For some more information (if needed), AJAX is interacting with a python server, and the Tables Constraints are as follow:
wp_users.username is a foreign key to webauthn_users.
webauthn_users.id is a foreign key to webauthn_credentials.
PS
The webauthn_registration function was working fine in a static HTML website that the submit function was directly linked to webauthn_registration.
Welcome to stackoverflow. I would suggest to try the following trick:
$('form').one('submit', function(e) {
e.preventDefault();
// do your things ...
// and when you done:
$(this).submit();
});

Ajax request reloads page in IE10

I read that IE10+ is able to do cross-domain normally like all the other browsers, but it does not seem to work in my case.
When I start the request, IE simply reloads the page.
ajaxreq = $.ajax({
postData = {"movie":movie, "season":season};
url: "/cgi-bin/find_data.py",
type: "post",
datatype:"json",
cache: false,
async : true,
data: postData,
success: function(response){
var json = $.parseJSON(response);
}
})
.fail(function(err) {
alert("error" + err);
});
The same happens with XDR, though I would need that only if I cared about IE<10.
There are two solutions:
Change the input type of the button from submit to button
Add return false; in the onclick event

Cordova/Phonegap + Restful Api service + POST method

Can someone please put some light on how to connect your Cordova App to WebServices hosted on a server.
Method: POST method
URL: http://192.168.1.20:3030/Service1.svc
Service Method: Login
Here's what I have been trying(Username-Password verification):
function GetLogin() {
var UName = "12345678";
var UPassword = "12345678";
var jData = { UserName: UName, Password: UPassword };
alert('1');
$.ajax({
type: "POST",
url: "http://192.168.1.20:3030/Service1.svc/AmalatLogin",
data: JSON.stringify(jData),
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: true,
success: function (msg) {
alert("Start");
document.getElementById("demo").innerHTML=msg.ResponseMessage.toString();
alert("End");
}
});
}
The above code is the javascript part. I have simply called the onClick="GetLogin()" on the submit button.
The problem is the code runs when the html file is being open using localhost( i.e. "http : //(localhost)/ aaa.html" (without spaces) or even by using Google Postman), but fails to do so when I run it from the directory(as in C:\www\aaa.html).
I have been searching for the solution everywhere. Please put some light on whether this is the limitation of technology or do I need to add some plugins.
Basically alert('1') is showing, other alerts are not.
They are all in same network

jquery ajax post not executed

I have a little problem with jQuery ajax.
I want to send a POST to a .php file and afterwards change the current URL. I tried the following code:
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
window.onload = function () {
$.ajax({
type: "POST",
url: "sample.php",
data: {'username': "STRING"},
timeout: 1000,
complete: function(){
window.location.href = 'http://127.0.0.1/sample/another.php';
}
});
}
</script>
So, as I can see in the chrome developer tools Network tab, no POST is executed. Although the URL changed to another.php
Can you tell me what I am doing wrong or how I can solve this issue?
use success instead of complete
$.ajax({
type: "POST",
url: "sample.php",
data: {'username': "STRING"},
timeout: 1000,
success : function(){
window.location.href = 'http://127.0.0.1/sample/another.php';
}
});
You will never see the POST. cause the url changes and remember the another.php hasn't made any AJAX requests! so you'll never find anything there. try changing the JS code in the success method to something like an alert to know if it works! and maybe then you'll find the POST method in the console!
Alternatively, you can use .done() as well. You may also think about scoping this inside a self executing function instead of a window.onload event.
(function($){
// Make an object for your param values.
var ajaxData = {
username: 'STRING',
anotherParam: 'STRING',
andanotherParam: 'STRING',
yetanotherParam: 'STRING'
};
$.ajax({
type: "POST",
url: "sample.php",
data: ajaxData
}).done(function(){
window.location.href = 'http://127.0.0.1/sample/another.php';
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}(jQuery));

AJAX success callback function not called

i'm working with python and js on a simple website.
i'm trying to call a method from the client side and get result, but no matter what i do
success function isnt happening.
this is my JS
$.ajax({
url: "http://127.0.0.1:8000/api/gtest/",
type: "POST",
data: { information : "You have a very nice website, sir."},
dataType: "json",
success: function(data) {
alert ("post is success");
},
error: function(request,error) {
alert(request.responseText);
alert(error);
}
});
this is my server side code
def gtest(request):
jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
return HttpResponse(jsonValidateReturn, content_type='application/json', mimetype='application/json')
The server responds to the call -
"POST /api/gtest/ HTTP/1.1" 200 31
tried to go over similar questions here but with no success :\
no matter what I do, only the error function is called.
the error alert is always empty.. no actual message.
I know this is probably a small change but I can't find it.
$.ajax({
url: "http://127.0.0.1:8000/api/gtest/",
type: "POST",
data: {
'information' : "You have a very nice website, sir.",
'csrfmiddlewaretoken': '{{csrf_token}}'
},
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function(data) {
alert ("post is success");
},
error: function(request,error) {
alert(request.responseText);
alert(error);
}
});
i cant upvote mccannf's comment.
The problem was solved by the link he posted, i ran the html code from a file on my pc and i needed to load it from the server so link wont start with file:// but with http://
best regards..

Categories

Resources