Ajax not working intermittently on some computers - javascript

I guess I'm not up-to-date or am missing something really simple.
I have the javascript code below, which uses ajax to connect to an external php file which reads the form post'd variables and pulls pricing from a sql database and returns the price, which is updated on the current page. The machine I am testing on uses Windows 7 and the latest version of Chrome browser. The ajax call ALWAYS works. I was in a different office yesterday and tested the code on a Windows 10 machine running the latest Chrome browser and the ajax call seems to never work and no price is updated. I also tried the latest Internet explorer browser on the same Windows 10 machine and it did NOT work. I found another Windows 7 machine at the same location using Chrome and it worked fine on it. What's even crazier is this morning, I tried the code on a Windows 10 machine running the latest Chrome and the first time the page loaded it worked but then quit again.
I keep checking the code and don't see anything, as it works fine on some machines. Any ideas or thoughts would greatly be appreciated.
I am using jQuery v1.12.4. I thought maybe this might have an issue so I tried using 3.1.1 even and nothing changed.
What am I missing? How would I be able to debug the ajax call to see what is actually happening on the computer that it isn't working?
THE JAVASCRIPT CODE:
<script type='text/javascript'>
// Monitor changes to Quantity form field
$('#Quantity').on('keyup', function(event) {
$.ajax({
url: 'updatePrice.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: $('#OrderDetails').serialize(),
datatype: 'json',
success: function(response) {
$("#showPrice").text('$' + response);
}
});
});
function updatePrice() {
$.ajax({
url: 'updatePrice.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: $('#OrderDetails').serialize(),
datatype: 'json',
success: function(response) {
$("#showPrice").text('$' + response);
}
});
}
</script>
THE SHORTENED HTML CODE RELATED TO THE JAVASCRIPT:
Quantity<br><input type="text" name="Quantity" id="Quantity" class="textWidth2" value="">
<img src="images/B&WSelected.png" id="BWimage" onclick="changeImage3(); updatePrice();">
<div class="border-top-color quote_panel">
<span class="quote_title">Your Price:</span>
<span id="showPrice" class="quote_price">$0.00</span>
</div>

I finally figured out what was causing the problem I believe. I think it is both 'crossdomain requests' related and encoding issues.
I edited the ajax call by removing the full url (http://www....) from url: and only left the file name and I removed the line
contentType: 'application/json; charset=utf-8',
from the code and now everything seems to work perfectly
function updatePrice() {
$.ajax({
url: 'updatePrice.php',
type: 'POST',
data: $('#OrderDetails').serialize(),
datatype: 'json',
success:function(response){
$("#showPrice").text('$'+response);
}
});
}

download jQuery CDN – latest stable version

Related

Ajax not working with a specific file name

Something really weird happened today and I cannot understand its behavior so if someone can help me to understand I would really appreciate it,
So I tried to do ajax call like below
var dto = {
'entity': 'SOME_VAL'
};
$.ajax({
type: 'GET',
url: '/WebService/checkout/applyCoupon.php',
dataType: "json",
data: dto,
contentType: "application/json; charset=utf-8",
cache: false,
success: function (response) {
console.log('Success!');
},
error: function() {
console.log('Error!');
}
});
and it returned my website's HTML code instead of JSON return values.
After a while I was able to resolve the issue by using the same code with diffrent file names.
url: '/WebService/checkout/applyCoupon.php', //NOT WORKING
url: '/WebService/checkout/applyCoupon2.php', //NOT WORKING
url: '/WebService/checkout/coupon.php', //NOT WORKING
url: '/WebService/checkout/apply.php', //WORKING
url: '/WebService/checkout/ap.php', //WORKING
It seems like it does not work when the file name contains coupon in it.
I have other websites running just fine using the exact same code with the file name applyCoupon.php so I do not understand why it won't work on a specific website. I can just simply use a different file name but I want to understand how/why it could happen. The environment is IIS. Can someone please help me to understand this issue? Thank you!

firefox not writing to database table but iexplorer and chrome do

I have written code that form checks and if all is good passes user input data to ajax so that the database table can be written to. While trying various browsers I am successful at writing to the table with chrome and iexplorer but nothing is written in firefox and no php errors are thrown.
Can anyone see any reason why the following ajax call is not successful with firefox?
$.ajax({
type: "POST",
url:"ajax/ajax_repSub.php",
data: {tDate: tDate, tSpecies: tSpecies, tSqrNum:tSqrNum, tLat:tLat, tLng:tLng, tCond:tCond, tNum:tNum, tHab:tHab, tBehav:tBehav, tReg: tReg, tStage: tStage},
dataType: "text",
cache: "true",
success: function(msg,string,jqXHR){
$("#results").html(msg+string+jqXHR);
}
});

In IE8, $.parseJSON is returning undefined

This is the strangest bug I've encountered.
I submit an ajax POST that would retrieve some data.
$.ajax({
url: url,
data: data,
type: 'POST',
success: function(data){
console.log(data)
},
dataType: 'json',
contentType: 'application/json; charset=utf-8'
}
)
In other version of IE and Chrome, the code would work fine and give the value of data which is {"success": true}.
But in IE8, data would return undefined . However, if I do JSON.stringify(data) , it would return {"success":true}.
I tried to convert the returned string to object via $.parseJSON(JSON.stringify(data) but it returned undefined again.
What can I do to get the response data as an object like I would normally in other browser?
EDIT:!! Found the solution. Apparently, the IE8 emulation of IE11 is a total crap and cannot display data correctly. Using IE8 on a virtual machine would correctly display the data and I figured out why it was undefined. Thanks for the help!
Found the solution! Apparently, the IE8 emulation of IE11 is a total crap and cannot display data correctly. Using IE8 on a virtual machine would correctly display the data and I figured out why it was undefined. Thanks for the help!
I'm not sure that you need to do that in IE8. Here is some code that I use with success in IE8 on a website that is live that is using jQuery 1.10.2:
$.ajax({
type: "POST",
crossDomain: true,
url: 'http://example.com/service/url',
data: params,
dataType: 'json',
timeout: 10000
})
.done(function( data ) {
console.log("Success");
console.log(data.success);
})
.fail(function( data ) {
console.log("Rejected");
console.log(JSON.stringify(data));
});
Hopefully this example is helpful for you to see how to handle the jQuery callback. The object that comes back is already an object if the result is actually valid JSON.
Updated this to be more generic of an answer.

Javascript FormData function works with Chrome and Opera but not Firefox

var data = new FormData(document.getElementById("uploadform"));
$.each($('#file')[0].files, function(i, file) {
data.append('file-'+i, file, this.name);
});
That code works on Chromium 26 but not Firefox 21. I put an alert within the each loop, "alert(this.name)", which displays the filename in Chrome, but nothing in Firefox...so that's apparently where it's all bogging down in Firefox.
I also tried it on Firefox 24--same problem. I know this code used to work on Firefox--an older version than 21.
This slight change, just leaving off a couple parameters, gives me the same results:
var data = new FormData();
$.each($('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});
I just have no idea what's up, because Mozilla docs show that this is supported and that I'm doing it right.
Here's the ajax call portion of the code if anyone curious (but this is not the problem--it's bogging down in the above code per my alert check):
$.ajax({
url: 'upload.php',
data: data,
contentType: false,
processData: false,
cache: false,
type: 'POST',
success: function(data){ }
Anyone have any similar problems, or have any inkling where to even look?
Are there errors in your browser's console? Check the Net (or Network) tab as well.
I would test in IE or Opera as well to confirm that it is specifically a FF issue.
Also try producing an alert
if (!window.FormData) {
alert('Doh!');
}

MVC Deployment - Relative Path issue with respect to Application Path

While deploying my MVC Project, I have faced an issue of relative path w.r.t server.
I am hosting the project as an application in IIS. Finally the url to my application will look like http://localhost/portal/Account/Login here 'portal' is the application name in IIS. In the ASP.net development server everything was working fine. while deploying it needed the relative path with respect to server. Because of this my jquery ajax requests started failing.
To fix this issue I kept the actions in hidden field and accessing from there and using for ajax request. The following is the code.
<input type="hidden" value="#Url.Action("GetNewOffersSearch", "Updates")" id="NewOffersSearchUrl" />
var NewoffersUrl = document.getElementById("NewOffersSearchUrl").value;
$.ajax({
type: 'GET',
url: NewoffersUrl ,
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});
Initially NewoffersUrl was "/Updates/GetNewOffersSearch" and it was throwing a path error. But now it is "/portal/Updates/GetNewOffersSearch" and its working fine
I just want to know the approach I am following is correct or not. Is there any better fixes for this issue?
The way we do AJAX requests is similiar, however we pass the URL directly to the url parameter of the ajax call rather than using a hidden field.
$.ajax({
type: 'GET',
url: #Url.Action("GetNewOffersSearch", "Updates"),
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});

Categories