var fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, 'start.html');
fs.readFile(filePath, { encoding: 'utf-8' }, function(err,data){
if (!err) {
console.log('received data: ' + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
} else {
console.log(err);
}
});
scroll по некоторым причинам (таким как некорректная работа скрипта и визуально некрасиво) использовать нельзя
function response($data = []) {
header("Content-Type: application/json; charset=utf-8");
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRETTY_PRINT;
$fails = implode('|', array_filter([
'\\\\',
$flags & JSON_HEX_TAG ? 'u003[CE]' : '',
$flags & JSON_HEX_AMP ? 'u0026' : '',
$flags & JSON_HEX_APOS ? 'u0027' : '',
$flags & JSON_HEX_QUOT ? 'u0022' : '',
]));
$pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
$callback = function ($m) {
return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
};
echo preg_replace_callback($pattern, $callback, json_encode($data, $flags));
exit;
}
response([ "data" => "Здесь русские буквы!" ]);
function response($data = [] {
header("Content-Type: application/json; charset=utf-8");
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRETTY_PRINT;
$fails = implode('|', array_filter([
'\\\\',
$flags & JSON_HEX_TAG ? 'u003[CE]' : '',
$flags & JSON_HEX_AMP ? 'u0026' : '',
$flags & JSON_HEX_APOS ? 'u0027' : '',
$flags & JSON_HEX_QUOT ? 'u0022' : '',
]));
$pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
$callback = function ($m) {
return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
};
echo preg_replace_callback($pattern, $callback, json_encode($data, $flags));
exit;
}
<div class="user" data-count="число_генерируется_движком">{news}</div>
$(".user").each(function (i, e) {
var count = $(e).attr("data-count");
// далее делаете с этим числом что хотите.
});
function response($data = []) {
header("Content-Type: application/json; charset=utf-8");
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRETTY_PRINT;
$fails = implode('|', array_filter([
'\\\\',
$flags & JSON_HEX_TAG ? 'u003[CE]' : '',
$flags & JSON_HEX_AMP ? 'u0026' : '',
$flags & JSON_HEX_APOS ? 'u0027' : '',
$flags & JSON_HEX_QUOT ? 'u0022' : '',
]));
$pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
$callback = function ($m) {
return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
};
echo preg_replace_callback($pattern, $callback, json_encode($data, $flags));
exit;
}
response([ "a" => "b", ]);
$.post('/some_page.php?', post).done(function (response) {
// обработчик json, в переменной response сразу распарсенный объект благодаря заголовку Content-Type: application/json
}).fail(function(xhr, status, error) {
console.error(error); // обработчик ошибки ответа сервера
});
//тут запрос для тестов
import Api from './index';
export default {
getInfo () {
return Api.get('https://api.coindesk.com/v1/bpi/currentprice.json')
}
}
var hash = window.location.hash;
var selectedOption = null;
if (hash && hash != '#') {
var parts = hash.split('=');
if (parts.length == 2 && parts[0] == "select_option") {
selectedOption = parts[1];
}
}