Im using ajax postback to post all fields of a form to a php page, that sets some sessions.
It all works fine when im working with firefox, but when i tried chrome and IE, it just does
nothing..
Here is my ajax:
$.ajax({
type: 'POST',
url: '../client_controller/teste',
data: {
form: $('#the-form-' + num).serialize(),
key: num,
},
//async: false,
//crossDomain: true,
success: function (response) {
alert(response);
},
error: function () {
console.log($.makeArray(arguments));
},
complete: function () {
console.log($.makeArray(arguments));
},
});
I saw some posts about this, but all the solutions i saw do nothing in my case.
The comented async and cross, were some of them.. And i dont want an empty data:, since i need to send those values.
Regards
Remove the trailing comma
complete: function() {
console.log($.makeArray(arguments));
}
, <------ THIS ONE
});
Already found the answer!
When using serialize functions be careful when using markup language.
My problem was that I had a form that wasn't closed, and although it worked on Firefox, the other browsers couldn't process the serialize function!
Related
I've got an form which posts a form trough an ajax script to some PHP code. Strangely enough everything is working in Safari, but once i try it in either Firefox or Chrome the ajax call handles everything as an error, though the console doesn't show any errors.
$('#newClearance').on("submit", function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
method: 'POST',
url: 'modules/avas/library/avas_functions.php?action=newClearance',
data: formData,
contentType: false,
cache: false,
processData: false,
success: function (data, status) {
$('#formModal').modal('hide');
$.notify(data, {type: 'success'});
$.get(window.location)
.done(function (r) {
var newDom = $(r);
$('#clearanceList').replaceWith($('#clearanceList', newDom));
});
},
error: function (data,status,error) {
$.notify(error, {type: 'danger'});
}
});
});
If I post the html form directly to the PHP script, everything goes well and the PHP script returns a success. It works in all browsers.
I really can't find the clue, especially as the same script, except for another form, is working perfectly fine in all browsers. Anybody a clue?
Additional info
Good thing to note might be that the form and jquery are placed in a modal, as you might notice form the code.
What do your server logs say when it receives the request?
Part of the solution appears to be the "submit" button of the form, which was required by the PHP script to run. I've removed this condition in the PHP script, but still wondering why firefox/chrome doesn't POST the submit button and safari does.
I am using a boostrap dialog box to pull in a list with a quantity. After I submit my form, and open the dialog box to check to see if the quantity has updated, it seems to be stale data. I have a call with ajax to the controller and then back to the database to pull in updated info. But when I set a breakpoint in the controller (on server side) it never hits. IT ONLY kicks out of the issue when I set a breakpoint to the function calling ajax within developer tools and debugger. I don't see any console errors either.
I don't have an issue with Firefox, just IE11.. here is the code:
<script type="text/javascript">
function LocationModal() {
$("#GetLocBtn").attr("disabled", "disabled");
var partNumber = $("#PartNum").val();
var Condition = 'Z';
var urlQry;
var receiveIsChecked = document.getElementById('Receive').checked;
var src = 'removed for security';
$.ajax({
type: "GET",
url: src,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { partNumber: partNumber, CCODE: Condition },
beforeSend: function () {
},
success: function (data) {
$("#LocationModalContainer").html(data.LocationModal);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
};
The problem is indeed that IE caches the results of Ajax calls. You can prevent that behavior either by adding cache: false to every call, like you've discovered, or setting it globally via ajaxSetup before you make any calls.
$.ajaxSetup({
cache: false
});
The use of ajaxSetup is discouraged in the jQuery documentation, but might be a good solution for you if you don't use any plugins that might rely on the normal behavior and want to quickly make sure none of your own ajax calls is cached.
Personally, I have my doubts about how real the interference risk mentioned in the documentation is when it comes to the cache setting, since basically you just make IE behave like other browsers.
I'm writing a jQuery for send list of form datas one by one to server.
It working perfectly on Firefox, but when i use chrome it's sending all data at once
Here is my code
$('.frmroldcon').each(function(){
var thobj=this;
$(thobj).prev().prev().prev('.failedicoconold').hide();
$(thobj).prev('.doneicoconold').hide();
$(thobj).prev().prev('.proccesicoconold').show();
fuser = jQuery('input[name="fusername"]',thobj).val();
fpass = jQuery('input[name="fpassword"]',thobj).val();
$.ajax({
type: "POST",
async: false,
cache:false,
url: "checkcon.php",
data: { checkcons: "1", fusername: fuser, fpassword: fpass},
success: function(data){
$(thobj).prev().prev('.proccesicoconold').hide();
if(data==0){
$(thobj).prev().prev().prev('.failedicoconold').show();
window.errc=window.errc+1;
}
else{
$(thobj).prev('.doneicoconold').show();
}
}
});
});
I'm using jQuery version 2.1.4, also tried other old versions like 1.4 but it seems not working.
Can anyone give me a solution for this or alternative for submit each forms data to server one by one,
Thanks a lot
A quick way to do serial, asynchronous, ajax requests using jQuery's $.when
var requests = [];
$('.frmroldcon').each(function() {
$.when.apply($,requests).then(function() {
requests.push($.ajax({/* your ajax request */});
});
});
You haven't shown the requirement to have to do these requests serially, however. And if you can do everything you need to do with 1 call to the server instead of 60, then that's the way you should be doing it.
Following code snippet:
$.ajax({
url: 'actions.xml',
cache: false,
dataType: 'xml'
}).done(function(data) {
$(data).find('script').each(function() {
deferreds.push($.ajax({
url: $(this).text(),
dataType: 'script'
}));
});
$.when.apply($, deferreds).then(function() {
$('.preloader').fadeOut();
...
});
});
On the browser, this code executes without a problem. The preloader fades out and everything is fine. On a mobile project via cordova, I get a problem. The $.when.apply part doesn't seem to execute. The preloader is never fading out and because of this the app is not working. Does anyone know, why this is happening?
The deferreds array has the same values as in the browser. I tested this via alert(JSON.stringify(deferreds));
I have figured this out by setting some alerts and could come down to this specific problem. It is definitely crashing at this point.
I have to finish this thing due today and I tried different things like setting timeouts, putting other things in function, etc, to solve this, but nothing seems to work and maybe an expert in this has the solution in two seconds.
You could avoid sending too many requests at same time but one by one using this kind of logic:
$.ajax({
url: 'actions.xml',
cache: false,
dataType: 'xml'
}).done(function (data) {
var queue = $.Deferred().resolve();
$(data).find('script').map(function () {
return $(this).text()
}).get().forEach(function (url) {
queue = queue.then(function () {
return $.ajax({
url: url,
dataType: 'script'
}).promise();
})
});
});
Be aware, maybe your issue is just because IOS simulator, you should test your original code on an IOS device (if not already done), and see if same issue persists.
I wrote a function in jquery and it works perfectly on localhost with the same setting
$('#resumey_graduation_add').live("click",function(){
var dataString=$('#job_form').serialize();
$.ajax({
type: 'GET',
url: 'app.php?name=job&op=resumey_graduation_add',
data: dataString,
cache: false,
beforeSend: function() {
$("#resumey_graduation_showall").html("<img src='images/loading.gif' />");
},
success: function(data2) {
$("#resumey_graduation_showall").html(data2);
}
});
return false;
});
but when moving it on my host , its not working and failed to load the data .
loading shows but not the result !
jquery versions are all tested and none of them worked
is there anything wrong , for expample not having JSON enabled or these things that are related to serialize() in jquery ?!
Since you are doing
type= get , data= $('#job_form').serialize(), url=Someurl.php?a=b&c=d
your url must be getting converted to
Someurl.php?a=b&c=d&props_from_form(name value pairs)
Do this way
$.ajax({
type: 'POST',
other things ....
sending form through GET may result in very big urls and that may get ignored by server.