PHP + jquery ajax json

PHP + jquery ajax json:

if (!function_exists('json_encode')) {  
    function json_encode($value) 
    {
        if (is_int($value)) {
            return (string)$value;   
        } elseif (is_string($value)) {
	        $value = str_replace(array('\\', '/', '"', "\r", "\n", "\b", "\f", "\t"), 
	                             array('\\\\', '\/', '\"', '\r', '\n', '\b', '\f', '\t'), $value);
	        $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
	        $result = "";
	        for ($i = mb_strlen($value) - 1; $i >= 0; $i--) {
	            $mb_char = mb_substr($value, $i, 1);
	            if (mb_ereg("&#(\\d+);", mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) {
	                $result = sprintf("\\u%04x", $match[1]) . $result;
	            } else {
	                $result = $mb_char . $result;
	            }
	        }
	        return '"' . $result . '"';                
        } elseif (is_float($value)) {
            return str_replace(",", ".", $value);         
        } elseif (is_null($value)) {
            return 'null';
        } elseif (is_bool($value)) {
            return $value ? 'true' : 'false';
        } elseif (is_array($value)) {
            $with_keys = false;
            $n = count($value);
            for ($i = 0, reset($value); $i < $n; $i++, next($value)) {
                        if (key($value) !== $i) {
			      $with_keys = true;
			      break;
                        }
            }
        } elseif (is_object($value)) {
            $with_keys = true;
        } else {
            return '';
        }
        $result = array();
        if ($with_keys) {
            foreach ($value as $k => $v) {
                $result[] = json_encode($k) . ':' . json_encode($v);    
            }
            return '{' . implode(',', $result) . '}';                
        } else {
            foreach ($value as $key => $v) {
                $result[] = json_encode($v);    
            }
            return '[' . implode(',', $result) . ']';
        }
    } 
}
function json_fix_cyr($var, $reverse = false)
    {
      if (is_array($var))
        {
          $new = array();
          foreach ($var as $k => $v) $new[json_fix_cyr($k, $reverse)] = json_fix_cyr($v, $reverse);
          $var = $new;
        }
      elseif (is_object($var))
        {
          $vars = get_object_vars($var);
          foreach ($vars as $m => $v) $var->$m = json_fix_cyr($v, $reverse);
        }
      elseif (is_string($var))
        {
          $var = $reverse ? iconv('utf-8', 'cp1251', $var) :  iconv('cp1251', 'utf-8', $var);
        }
      return $var;
    }
function tojson($var){
	return json_encode(json_fix_cyr($var));
}
 
 
header("Content-Type: text/javascript; charset=windows-1251");
 
echo tojson(array('key' => 'value'));
 
 
die(); // stop executing script
//exit;
jQuery(function($){
	$('.scroll_top').click(function () {
		$.ajax({
			url: "ajax.php",
			type: "POST",
			dataType: "json",
			data: {'key': '12321'},
 
			complete: function(xhr, status) {
				alert('End');
			},
 
			success: function(data, status) {
                alert( data.key );
			},
 
			error: function(xhr, status) {
				alert( 'Error' );
			}
		});
	});
});

Leave a Reply

Your email address will not be published. Required fields are marked *