in my application I have to make an ajax call to php file.it works proper in all devices. but when I tried it on ipad mini it not calls the php, so that the functionality not works, I've seen so many question about this problem and edited my code like this.
jQuery.ajax({
type: "POST",
async: true,
cache: false,
url: "directory/phpfile.php",
data: data,
success: function(response) {
}
});
my old code is
jQuery.ajax({
type: "POST",
url: "wp-admin/admin-ajax.php",
data: data,
success: function(response) {
}
});
and the problem still cant resolve . so please any one tell me how to resolve this.
Please use this code
$("#ajaxform").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
//data: return data from server
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
}
});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
$("#ajaxform").submit(); //Submit the FORM
<script type='text/javascript'>
$(document).ready(function startAjax() {
$.ajax({
type: "POST",
url: "test.php",
data: "name=name&location=location",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
Related
I am creating a slim project using CRUD. I am trying to get 1 user, by using GET. When I use .append it returns data like this: the name is the request getting data from the database.
undefined
name
undefined
name
Obviously I only want one user to be shown, however when I add .html instead of .append it just comes back undefined.
This is my code that is coming back undefined
$(document).ready(function showStaff(processResponse) {
$.ajax({
type: 'GET',
dataType: "json",
url: "borrower.php/borrowers",
success: showAllBorrowers,
error: showError
});
});
//shows borrowers in HTML
function showAllBorrowers(responsedata){
$.each(responsedata.borrower,function(index,borrower){
$("#borrower_list").append("<li type='square'> Borrower Id:"+borrower.borrower_id+",FullName:"+borrower.first_name+" "+borrower.last_name+", Email:"+borrower.email);
$("#borrower_list").append("</li>");
});
}
$(document).ready(function(){
$("#btnSaveBorrower").click(function(){
var borrower=new Borrower($("#firstName_add").val(),
$("#lastName_add").val(),$("#email_add").val());
$.ajax({
type: 'POST',
dataType: "json",
url: "borrower.php/borrowers",
data:JSON.stringify(borrower),
success:function(data){
console.log(data)
location.reload();
},
});
});
});
function Borrower(firstName, lastName, email){
this.firstname=firstName;
this.lastname=lastName;
this.email=email;
}
$("#btnSearch").click(function(){
var id = $("#StaffBorrower_Id").val();
$( "#update" ).show();
$( ".delete" ).show();
$("#btnUpdateBorrower").show();
$("#btnDeleteBorrower").show();
$.ajax({
type: 'GET',
dataType: "json",
url: "borrower.php/borrowers/" + id,
success: showResponse,
error: showError
});
});
function showResponse(responsedata){
var item = $("<li>"); item.text("Borrower Id:"+responsedata.borrower_id+",FullName:" +responsedata.first_name+" "+responsedata.last_name+", Email:"+ responsedata.email);
$("#search_borrower").html(item);
item.attr("type", "square");
item.attr("data-delete-id", responsedata.borrower_id); item.attr("data-update-id", responsedata.borrower_id);
console.dir(responsedata);
}
$("#btnDeleteBorrower").click(function(){
var result = confirm("Are you sure you want to delete?");
if (result) {
var staff= delete Borrower($("#borrower_Id").val(), $("#firstName").val(), $("#lastName").val(), $("#email").val());
$.ajax({
type: 'DELETE',
dataType: "json",
url: "borrower.php/borrowers/"+$("[data-delete-id]").data("delete-id"),
success: function(res){
console.log(res);
location.reload();
},
error: showError
});
}
});
$("#btnUpdateBorrower").click(function(){
var result = confirm("Are you sure you want to update?");
if (result) {
var borrower = new Borrower ($("#firstName").val(),
$("#lastName").val(),
$("#email").val());
$.ajax({
type:'PUT',
dataType:"json",
url:"borrower.php/borrowers/"+$("[data-update-id]").data("update-id"),
data:JSON.stringify(borrower),
success:function(data){
console.log(data);
location.reload();
},
error: function(){
console.log("There seems to be an error")
}
});
}
});
The console log is working, I am getting the data in the console but it isn't being returned in the html for some reason. Does anyone have any ideas why? Thanks
I have a situation where I make an ajax call, on returning from which (successfully), I make an another ajax call with the returned data and submit the page. The pseudo code looks something like this:
$.ajax({
url: 'first_page.jsp',
type: 'POST',
dataType: 'JSON',
data: {...}
}).done(function(response) {
$.ajax({
url: '2nd_page.jsp',
type: 'POST',
dataType: 'JSON',
data: {...}
});
thisPage.submit();
});
The inner ajax call is not executed if I do not comment out the 'submit' line. I do not understand the behaviour. Can someone please help me with this.
Thanks
You need to execute submit on second ajax sucess.
For example
$.ajax({
url: '',
type: 'GET',
data: {
},
success: function (data) {
$.ajax({
url: '',
type: 'GET',
data: {
},
success: function (data) {
//do your stuff
}
$("input[type='submit']").click(function(event){
event.preventDefault();
$.ajax({
url:'abcd.php',
method:'post',
type:'json',
async:false,
success:function(response){
$.ajax({
url:'abcde.php',
method:'post',
type:'json',
data:{res:response},
success:function(response){
console.log(response);
}
});
$("form").submit();
}
});
});
Try this one.Its working correctly.
I have the below ajax call and sometimes the request stops the page from loading as the data being passed is undefined.
I there a way to put a condition to handle the request if it has values that are undefined?
Can it be wrapped with a if condition?
newuser is not defined
$.ajax(
{
url: 'sample.aspx,
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data){
...
} ,
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
});
Simplest way would be to use a pipe:
$.ajax({url: 'sample.aspx',
data: newuser || {},//continue here...
If your variable was not initialized, empty object will be sent instead.
That's if and only if you can handle empty "newuser" for some reason.
I'm assuming that not closed URL is just a mistake in copy-paste, and not actually part of your code.
how about
if(newuser)
{
$.ajax(
{
url: 'sample.aspx,
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data){
...
} ,
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
});
}
If you are unable to handle an empty newuser you can try something like this:
if (newuser) {
$.ajax({
url: 'sample.aspx',
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data) {
...
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
}
For some reason I can't make this code work, I am trying to reload/refresh the page using GET. All the sample codes that I have found uses POST but I can't go to that because our forms require GET.
This is my code:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
document.location.reload();
//location.reload(true);
//$("#acceptDiv").html(html);
}
});
It should be a simple way to get this done, why this is so easy with POST and not with GET?
Here we go:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
window.location.href= "../yourUrl";
}
});
Hope it helps;)
if you have not any other error in your script( please go to console in your browser press F12),
then you can try with location.reload();
in your success method, like that:
$.ajax({type: "GET",
url: "CorrectResults",
data: data,
beforeSend: function() {
console.log("Submitting Data");
},
cache: false,
success: function(html) {
location.reload();
}
});
I am trying to submit a form without reloading the page. I have tried using this script with no success:
$('#formId').submit(function(e){
$.ajax({
type: "POST",
url: 'serverSide.php',
data: $('#formId').serialize(),
success: function(data){
alert(data);
}
});
e.preventDefault();
});
Does anyone know what I am doing wrong?
Using return false will usually do the trick
$('#formId').submit(function(e){
$.ajax({
type: "POST",
url: 'serverSide.php',
data: $('#formId').serialize(),
success: function(data){
alert(data);
}
});
e.preventDefault();
return false;
});
Try putting preventDefault before the AJAX. This way the default actions are prevented before the ajax-request is triggered
$('#formId').submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: 'serverSide.php',
data: $('#formId').serialize(),
success: function(data){
alert(data);
}
});
});