Menu

How to make jQuery ajax (JSON) requests

html file

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <script type="text/javascript" src="./jquery-1.6.2.min.js"></script>
        <title></title>
        <!-- // phpjs   >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
            <script src="./php.default.min.js" type="text/javascript"></script>
        <!-- // phpjs   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  -->
    <link href="./main.css" rel="stylesheet" type="text/css">

 <script type="text/javascript">
var GET_ajax_file_rest = "manager.php";
var name = "test";
var select = "select";
var get_data = 'l=de';
get_data += '&action='+select;
get_data += '&name='+name;
$.ajax({
 type: "GET",                                                         
 url: GET_ajax_file_rest,
 async:true,
 dataType: "json",
 data: get_data,
 success:  show
});// .get                                               
function show(data) {
 alert(data.user_name);
}
</script>
<body>
</body>
</html>

php curl file (manager.php)

<?php
/**
 * Send a POST requst using cURL
 * @param string $url to request
 * @param array $post values to send
 * @param array $options for cURL
 * @return string
 */
function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

/**
 * Send a GET requst using cURL
 * @param string $url to request
 * @param array $get values to send
 * @param array $options for cURL
 * @return string
 */
function curl_get($url, array $options = array())
{
    $defaults = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 4
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

$options = array
(
   // CURLOPT_HEADERFUNCTION => 'read_body'
  //  CURLOPT_HEADERFUNCTION => 'read_header'
);

function read_header($ch, $string)
{
    $length = strlen($string);
    echo "Header: $string<br />\n";
    return $length;
}

function read_body($ch, $string)
{
    $length = strlen($string);
    echo "Received $length bytes<br />\n";
    return $length;
}



$appl_user=trim($_GET['name']);
$appl_action=trim($_GET['action']);
$appl_server= "server ip";


$url ='http://'.$appl_server.'/seminario/seminario-svisto.php?action='.$appl_action.'&user='.$appl_user;
$result = curl_get($url,$options);
if ($result){

    echo $result;

}
else{

    $data = array('success'=> false,'user'=>'9:9');

    echo json_encode($data);
}

?>
Posted by root apostolos 2012-05-09 | Draft

Log in to post a comment.