ajax in javascript not working. not reading the code - javascript

I'm calling a simple JS script to load ajax functions.... and it doesn't work... I debugged using document.write to see where the problem lied and I can see my TEST 1 but not my TEST 2...
MY HTML
<html>
<title>Ajax Infinite scroll using jQuery - InfoTuts</title>
<head>
<!--<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img id='loading' src='img/loading.gif'>
<div class="masonry" >
<div id="demoajax" cellspacing="0"></div>
</div>
</body>
<script type="text/javascript" src="script.js"></script>
</html>
MY JavaScript
var ajax_arry=[];
var ajax_index =0;
var sctp = 100;
$(function()
{
$('#loading').show();
/* document.write("TEST1"); */
$.ajax(
{
/* document.write("TEST2"); */
url:"scroll.php",
type:"POST",
data:"actionfunction=showData&page=1",
cache: false,
success: function(response)
{
$('#loading').hide();
$('#demoajax').html(response);
}
});
/* MORE CODE BELOW */
my document.write TEST 1 prints on screen, but my TEST 2 can't print..
It seems it's not going in the $ajax( section of the code

Do not use document.write to debug. Browsers have a console. Use console.log and you can set break points.
And you are creating an error putting document.write in the middle of an object!!! That is a syntax error.
If you are trying to figure out why the Ajax call is not loading, add an error handler and see if it is getting called.
$('#loading').show();
console.log("loading shown");
$.ajax({
url: "scroll.php",
type: "POST",
data: "actionfunction=showData&page=1",
cache: false,
success: function(response) {
console.log("success", response);
$('#loading').hide();
$('#demoajax').html(response);
},
error: function() {
console.log("error", arguments);
}
});

You have missed to close your function;
put this }); after your ajax call.
if you got "500 (Internal Server Error)", you may have to change the Permissions of that PHP file.

try by putting document.write("TEST2"); inside success function.

Put $.ajax on document.ready function
$(document).ready(function(){
$.ajax({
/* document.write("TEST2"); */
url:"scroll.php",
type:"POST",
data:"actionfunction=showData&page=1",
cache: false,
success: function(response){
$('#loading').hide();
$('#demoajax').html(response);
}
});
});

Related

AJAX not working, updating database via JS

I've made game in phaser (JS library for games) and i now want to make the scoretable with JS/PHP. What interest me is to pass a variable from js to php to update the database. I've read many topics and all the answers lead to AJAX and this example:
function score_submitting() {
var var_data = "Hello World";
$.ajax({
url: "submit.php",
type: "GET",
data: { var_PHP_data: var_data },
});
}
But it's not working. Im sure that the function can be called, cause when I put alert there, it works. But with AJAX happens nothing. The file is in the middle, cause it comes from game:
(HTML)
(...)
<body>
<center>
<div id="gra">
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript" src="create.js"></script>
<script type="text/javascript" src="update.js"></script>
<script type="text/javascript" src="game.js"></script>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
</div>
</center>
</body>
(...)
Thanks for answers!
If you are using GET request you should make your request like that :
function score_submitting() {
var var_data = "Hello World";
$.ajax({
url: "submit.php?mydata="+var_data,
type: "GET"
});
}
Or you can use POST request if you don't want to pass your parameters in URL.
You can put success and error function for testing your code like following
you can also fetched data that is returned from ajax call as returnedData
function score_submitting() {
var var_data = "Hello World";
$.ajax({
url: "submit.php",
type: "GET",
data: { var_PHP_data: var_data },
success:function(returnedData)
{
alert('success');
},
error:function()
{
alert('Error');
},
});
}

Ajax Requests don't work when other JS scripts are included in the html page

I am using an AJAX request to get some data fetched from the database. There are four .js files that I have included in the HTML page. They are
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jqueryui.js"></script>
<script type="text/javascript" src="scripts/framework.plugins.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".buttonid").click(
function(){
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "php/fetch_candidates.php",
async: true,
dataType: "html", //expect html to be returned
error: function(){
return true;
},
success: function(response){
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
</script>
This doesn't work. But if I remove
<script type="text/javascript" src="scripts/framework.plugins.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>
then the AJAX starts working. How can I solve this as I need to keep all the .js files in the html page.
There is a responsecontainer div which handles the html afte rthe ajax request. The problem is only with including those two js scripts. TIA
When the other frameworks also use $ function, it might override jQuery's $ functional behaviour. But this leaves the jQuery variable untouched. So try using a closure and execute it inside an IIFE:
(function ($) {
$(document).ready(function() {
$(".buttonid").click(function () {
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "php/fetch_candidates.php",
async: true,
dataType: "html", //expect html to be returned
error: function(){
return true;
},
success: function(response){
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
})(jQuery);
This will translate all the $ into jQuery's original function and make sure you use $ with only jQuery and not any other framework like PrototypeJS or Scriptaculous.
Try using var jq = $.noConflict(); , just before the $(".buttonid").click(
and use jq instead of $, it looks like a conflict issue, can you please share what is inside the framework.plugins.js and custom.js

Ajax Automatic Div Update w/out page refresh won't work

I am struggling to figure out why this is not working, but I would like it so that the information from load.php is placed in the div sample but if that code is updated (say if i was pulling information from a database), only the information included in load.php would refresh - not the entire page.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
<script>
$(document).ready(function(){
var callAjax = function(){
$.ajax({
method:'get',
url:'load.php',
success:function(data){
$("#sample").html(data);
}
});
}
setInterval(callAjax,5000);
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id='sample'>100 </div>
</body>
</html>
I had a similar situation with my AJAX calls. I am not sure if your syntax is actually bad but here is how my Ajax call looks for a similar partial load.
var callAjax = function(){
$.ajax({
url: "load.php"
type: "GET",
async: true,
success: function (data) {
$("#sample").html(data);
}
});
}
};
setInterval(callAjax,5000);
I found that my Ajax started working once I added the async:true and made sure my controller was handling GET requests. I had previously forgotten that it was set to HTTPPOST only.
I hope this helps out. Not sure if it will.
cheers.
setInterval('someFunc()', 1000);
function someFunc()
{
$.ajax({
async: true,
type: "GET",
url: "www.domain.com/url",
data: data,
success: function (html) {
$("#myDiv").html(html);
}
});
}

Trigger successhandler from ajax call when someone clicks a button

I have an ajax call which grabs data from a php file that connects to an api. I have a success handler that receives the data from the ajax call and then loads a function from the API library. The issue is, I want the user to click a button and then run the function inside of the success call (using the same data from the ajax call earlier).
I believe my ajax call needs to stay inside the window.onload = function(), it doesn't work if I try to run the ajax call when the user clicks the button.
<html>
<head>
<title>hi</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script>
function hello(){
alert("hi");
}
function successHandler(data){
screenleap.startSharing('DEFAULT', data, {
nativeDownloadStarting: hello
});
}
window.onload = function() {
$.ajax({
type: "POST",
url: "key.php",
data: '',
success: successHandler,
dataType: "json"
});
};
</script>
</head>
<body>
<input type="submit" value="submit" id="submit">
</body>
</html>
Please let me know how I can do this, any help would be greatly appreciated.
Update:
so I have tried adjusting my code, but for some reason nothing happens anymore when I click the button. It doesn't even shoot out my console.log confirming I had even clicked the button.
<html>
<head>
<title>hi</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
<script>
var dataFromServer;
function hello(){
alert("hi");
}
function successHandler(data){
dataFromServer = data;
console.log("data from server: ")
console.log(dataFromServer);
}
window.onload = function() {
$.ajax({
type: "POST",
url: "key.php",
data: '',
success: successHandler,
dataType: "json"
});
};
$("#submit").click(function(){
console.log('clicked submit');
if(dataFromServer)
{
console.log(datafromserver);
screenleap.startSharing('DEFAULT', dataFromServer, {
nativeDownloadStarting: hello
});
}
});
</script>
</head>
<body>
<input type="submit" value="submit" id="submit">
</body>
</html>
all the best,
-- 24x7
You can store data to some global variable and on button click use same variable.
var dataFromServer;
function successHandler(data){
dataFromServer = data;
}
jQuery("#submit").click(function(){
if(dataFromServer)
{
screenleap.startSharing('DEFAULT', dataFromServer, {
nativeDownloadStarting: hello
});
}
});
Update:
Instead of window.onload use jquery onload function to populate data.
http://jsfiddle.net/ZGggK/
$(function() {
$.ajax({
type: "GET",
url: "https://api.github.com/users/mralexgray",
data: '',
success: successHandler,
dataType: "json"
});
});

How to post zk url through javascript

I am using JavaScript with ZK framework. In some scenario, I want to post URL from JavaScript in ZK. How to call ZUL file from JavaScript?
Is there any way to post URL using JavaScript in ZK?
Assume you have a content.zul and you want to get it via ajax in javascript, this can be done by using zk built in jquery in zul page or using jquery in pure html.
zul sample:
<zk>
<script><![CDATA[
function loadContent () {
jq.ajax({
url: "content.zul",
type: "post",
// callback handler that will be called on success
success: function(response, textStatus, jqXHR){
jq('$content').html(response);
}
});
}
]]></script>
<div id="content"
onCreate='Clients.evalJavaScript("loadContent();");' />
</zk>
html sample:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function () {
$.ajax({
url: "content.zul",
type: "post",
// callback handler that will be called on success
success: function(response, textStatus, jqXHR){
$('#content').html(response);
}
});
}
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>

Categories

Resources