简体   繁体   中英

ajax with jquery and json and php file. is it advisable to use json here?

I have not worked with json yet. and have no idea how it works I want to use jquery for performing ajax with php file. is it advisable to use json here. heard from someone that json is light weight. Is that true? if yes how would i use json for ajax-php peration with jquery

JSON is an information interchange format based on JavaScript literal notation (but is actually a subset of literal notation, you may only use strings, numbers, objects, booleans and arrays, from memory).

Unless you need to receive data back, you won't use it. It is lightweight compared to something like XML.

If you need to get JSON using jQuery, you can use the high level AJAX API call called getJSON() which is as simple as....

jQuery

$.getJSON('path/to/whatever.php', function(obj) {  
    alert(obj.name);
});

PHP

header('Content-Type: application/json');
echo json_encode(array('name' => 'bob'));

If you wanted to throw away requests that don't look like AJAX, use this...

if (isset($_SERVER['X-REQUESTED-WITH']) AND $_SERVER['X-REQUESTED-WITH'] !== 'XMLHttpRequest') {
   die('XHR only.');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM