php - why isn't my AJAX get request working -


in js page want variables php page using ajax(); triggered html page load. i've tried use both , post, nothing alerts or logs console supposed to, not error.

html:

<!doctype html> <html>     <head>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>     <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>    <script src="http://xxxxxx/bn/sample.js"></script>     </head>     <body>         <div>welcome! </div>     </body> </html> 

js: (sample.js)

$(function(){   $.ajax({         url : "http://xxxxxx/bn/sample.php",         type : 'get',         data : data,         datatype : 'json',         success : function (result) {            alert(result.ajax); // "hello world!" should alerted            console.log(result.advert);          },         error : function () {            alert("error");         }     });      }); 

php:

<?php      $advert = array(         'ajax' => 'hello world!',         'advert' => 'working',      );      echo json_encode($advert); ?> 

the data set @ "data: data" ist data u send php script. u dont send any. data receive available in success function. print it. plus removed alert. alerts lame. console rocks!

the " $(document).ready(" part starts function document completlty loaded. guess need , wanted there.

$(document).ready(function() {      $.ajax({         url : "http://xxxxxx/bn/sample.php",         type : 'get',         data : (),         datatype : 'json',         success : function (data) {            console.log(data.advert);          },         error : function () {            console.log("error");         }     });      }); 

edit: remove last comma, array ends there. want set header before output.

<?php  header('cache-control: no-cache, must-revalidate'); header('content-type: application/json');       $advert = array(         'ajax' => 'hello world!',         'advert' => 'working'      );      echo json_encode($advert); ?> 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -