I'm writing code to show notification by using Ajax to access database. However, when I refresh the Home page, there isn't any change to my page even after I've written 'alert' command. Please help me how to solve this kind of problem.
PS: I also have another ajax code written in my project and their working fine.
<script type="text/javascript">
$(document).ready(function(){
setInterval(function(){
$.ajax({
url: "<?php echo site_url('Jobs/jobNotification');?>",
type:"POST",
dataType:"json",
data:{},
success: function(data){
alert(data.msg);
}
});
}, 2000);
})
Related
I use ajax reload for my table.php page. I`m using material.min.js from google material design for some features like tooltips. But after the page reload with ajax, material.min.js stop working.
My index.php include table.php, where I have:
<script>
$(document).ready(function(){
setInterval(function(){
$.ajax({
url: "table.php",
cache: false,
success: function(body){
$(".page-content").html(body);
}
});
},10000);
});
</script>
Thanks for all, I have find the answer with adding this:
componentHandler.upgradeAllRegistered();
I got a simple php page that executes a Jquery script as follows:
SCRIPT part:
<script>
$(document).ready(function(){
$(".ajax_link").mouseover(function(){
sfpRx($(this).text());
});
}
function sfpRx(sfp){
console.log("sfpRx Was executed");
$.ajax({
type: "GET",
url: "/NOKIA/sfp-load.php?sfp="+sfp,
async: true,
dataType: "text",
timeout: 5,
success: function(data){
console.log(data);
}
});
}
</script>
HTML part:
<td class="ajax_link">3FE62600AA</td>
sfp-load.php content:
<?php echo $_GET['sfp'];?>
Explanation: everytime I mouse over the HTML part on the page, it executes the sfpRx() function that makes an ajax call to sfp-load.php. This Php page should return "3FE62600AA" to the ajax call, which should get printed in the console.
Problem: the "3FE62600AA" never gets printed in the console but I'm certain that the sfpRx() function is called because it prints "sfpRx Was executed" in the console. What could be happening? Am i forgetting anything? I'am sure the sfp-load.php page is in the NOKIA/ folder. I'am really annoyed with this.
Im beginner in JS & AJAX so please bear with me.
I use codeigniter framework in this project. I want to create a datepicker and passing its value through AJAX. This is the script :
<script>
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true
});
$("#btn_insert").click(function() {
var url = "<?php echo base_url();?>" + "index.php/backend/umat";
$.ajax({
type: 'POST',
url: url,
dataType: "json",
data: $('#my_form').serialize(),
success: function(data) {
alert(data);
},
error: function(xhr, status, error) {
/*var err = eval("(" + xhr.responseText + ")");
alert(err.Message);*/
alert("a");
}
});
});
});
</script>
The datepicker is already shown & working fine. However the AJAX is still not working. When i tried to alert the error (the one i commented in code), it doesnt alert anything. So i tried to alert("a"); and yes, "a" is alerted so theres something wrong in the AJAX. The data is never alerted which means it never success.
Theres no error in the console. Im using chrome browser.
Any help is appreciated and please just ask me if you need something. Thanks :D
Can you try writing this url in you browser http://mysite.com/index.php/backend/umat and show information? check your htaccess perhaps index.php is delete
UPDATE:
I need to get the jason.. The click event doesnt work why..
update:
<script>
$(document).ready(function(){
$("#RefreshButton").click(function(){
$.ajax({
url: "/test2/ajax.php",
type: "GET",
dataType: "json",
success: function(data)
{
alert("This is my data: "+data);
$(".article").remove();
$.each(data, function(key, value){
$('articleColumn').append( '<p class="article"><font size="5"><b>'+value[0]+'</b></font><br/>'
+value[1]+' Read more...</p>');
});
},
error: function( error )
{
alert(JSON.stringify(error));
}
});
});
});
</script>
The ajax call works..but not when it is in the click event handler..why?!?
I think the solution to the problem lies in the html:
<a href="" id="RefreshButton" >Refresh</a>
may be it refreshes the page and then send the response. I think it is the problem in the way the event propogates ..hmm
Generally when making an ajax call using jQuery I use the short hand version of POST and GET methods. So in your case I would do something like this
$.get("ajax.php", function(data){
alert(data); //just to make sure it works
}, "json");
Be sure to send the response back from ajax.php as json using json_encode(array("key"=>"value","key"=>"value")); ?>)
Also since ajax cannot go across domains you don't have to specify http://localhost/ajax.php, rather you can just specify it as the relative path to where you are calling the jquery function from.
I'm trying ajax for the first time but it doesn't work.
This is "some.php" which handles the ajax call:
<?php
echo "success";
?>
And this is the javascript that calls it:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">
var msg;
$.ajax({
type: "POST",
url: "some.php",
data: ({ })
success: function(msg){
alert( msg );
}
});
</script>
Can you see where the problem is?
I should state I'm working under wordpress and both files reside in \wp-content\themes\twentyten (maybe the url in the ajax call is wrong?)
First of all remove the data:({}) which is pointless. you are also missing a , behind your data statement. this is most likely the issue.
if both the files is in the same directory, then the url should be correct.
However, i urge you to use a tool like FireBug in order to debug your problem further
You should run your script when the page has loaded (more precisely, when the DOM is ready). jQuery offers an event for that.
Your code could then look something like this:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "some.php",
data: ({ })
success: function(msg){
alert( msg );
}
}
});
Two things to do:
register a .fail callback. The code as it is will just call the alert() if it succeeds, otherwise, errors are not raised. See http://api.jquery.com/jQuery.ajax.
check the web server log to see if some.php is exec'd and if so, what errors may be occurring on the server.