JavaScript Code executes even after commenting - javascript

I am trying to develop a small CRUD project using ASP .Net MVC as part of my learning experience, I am trying to use jQuery and Ajax to get the data and display it.
I am new to JavaScript and ajax, not sure If I am commenting it the wrong way.
In the following bit of code, I have commented the
$('#btnUpdate').show();
$('#btnAdd').hide()
functions, but they are still getting executed on the page.
function getbyID(CusId) {
$('#CustomerName').css('border-color', 'lightgrey');
$('#Age').css('border-color', 'lightgrey');
$('#Address').css('border-color', 'lightgrey');
$.ajax({
url: "/Customers/getbyID/" + CusId,
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (result) {
$('#Id').val(result.Id);
$('#CustomerName').val(result.CustomerName);
$('#Age').val(result.Age);
$('#Address').val(result.Address);
$('#myModal').modal('show');
//$('#btnUpdate').show();
//$('#btnAdd').hide();
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
return false;
}
I cleaned the solution and build it again but the code is still getting executed. Is there a different way of commenting the code.
Well The problem is solved now, I cleared the browser Cache and it has started working. As I am new, I was not aware of the execution style of javascript.

Related

Only IE 11 losing focus on ajax call sometimes ASP.NET MVC4

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.

jQuery when, then executing in browser but not on mobile device?

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.

Why is my jquery ajax not working on my page and page is also refreshing

I am new in the area of jQuery/Ajax and my little test function doesn't work. And my page is also refreshingcan any one help me
<script type="text/javascript" >
$(document).ready(function() {
$("#ser_itm").change(function() {
var id=$(this).val();
var dataString = 'id='+ id;
alert(dataString);
$.ajax({
type: "POST",
url: "bar_pull.php",
data: dataString,
cache: false,
success: function(html) {
$("#tbl").html(html);
}
});
});
});
Pass the function, not the result of the function call:
$('.linkDetails').on('click', getDetailsFromServer);
Apply the same to your AJAX success callback:
success: postToPage
Also, the getDetailsFromServer() function needs to be defined before you bind it to an event. Move the function declaration before your .on('click', ...) call.
So I'm going to try and explain these points more clearly:
You cannot access C:\Users\yah\Desktop\text.txt. This is a server side path, your javascript runs on the client side. So this needs to be a path you can browse to in your browser, something like /pathinURL/text.txt. How you do this is dependant on your hosting technology, etc.
Your call backs are also wrong,
$('.linkDetails').on('click', getDetailsFromServer());
&
success: postToPage()
these will execute the function when they are hit, (well it actually binds the function result) not when the event happens. To make these work you need to remove the braces:
$('.linkDetails').on('click', getDetailsFromServer);
&
success: postToPage
this then hooks up the actual functions as function pointers and thus the actual functions will be fired when you want them to be.
so your final code will look like:
$('.linkDetails').on('click', getDetailsFromServer);
function getDetailsFromServer() {
$.ajax({
type: 'GET',
url: '/someURL/text.txt',
success: postToPage
});
}
function postToPage(data) {
$('.textDetails').text(data);
console.log(data);
}
what Arun P Johny said is right! but your code has another probloem
$('.linkDetails').on('click', getDetailsFromServer);
try above
The same origin policy implemented by browsers prevents local file system urls... if the page and the files are in same folders it might work.
See SOP for file URI for FF

Ajax calls from javascript are not working in drupal 7? Am I doing any thing wrong

function updateRecord(actdate, plandate, bugnumber) {
var adate = document.getElementById(actdate).value.split("-");
var pdate = document.getElementById(plandate).value.split("-");
jQuery.ajax({
url: '?=bug_parser/updaterecord/'+adate[0]+'/'+adate[1]+'/'+adate[2]+'/'+pdate[0]+'/'+pdate[1]+'/'+pdate[2]+'/'+bugnumber,
dataType: 'html',
async:true,
crossDomain:true,
success: function(response, textStatus, jqXHR) {
if(response == '1') {
alert("success");
} else {
alert("Failed");
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error updating the record');
},
});
return false;
}
I am trying to update a record through ajax call on button click event. Have proper menu call backs in my drupal module. Interesting thing is that, if I copy paste the url in browser navigation bar, it works fine as expected. But if I make ajax call through javascript it doesnot work
The reason your ajax is not working is because you need to put it inside a drupal behavior.
Lookup drupal js behaviors. It's basically creating a wrapper for your js so it works well with the drupal javascript files and includes.
You can also use this topic as a reference for behaviors: Drupal behaviors
Normally all your javascript has to go inside a behavior. It's best practice and saves you a lot of headaches

qunit is not returning ajax result from a javascript function with titaniuam

i careate a function in javascript in titanium.
function getdetail(Success){
alert("hit the function");
var detail[];
$.ajax({
type: "GET",
url: "Detail.xml",
dataType: "xml",
success: function(xml) {
alert(xml);
detail.push(xml);//fill data by finding root each
sucess(detail);
}
});
return detail;
}
by qunit test i want to test this ,
test("test for detail",function(){
stop();
getdetail(function(detail){
start();
ok(true);
})
})
But some how it give me undefined value, null value, because i placed a alert inside the ajax function. this alert was not hit. then i place a alert outside the ajax function this was hit.
whole work i am doing with apslator titanium developer using aptana studio.
how can i test this javascript titanium function function
First thing I would do is install Titanium Studio either on top of Aptana or standalone and then start using the Ti.API.* for you're debugging instead of alerts. Also what exactly are you trying to accomplish. I can't tell from you're question what you are trying to do fully?

Categories

Resources