开发者

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 fro开发者_Go百科m 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.');
}


A simple example

http://www.electrictoolbox.com/json-data-jquery-php-mysql/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜