<script type='text/javascript'>
$( document ).ready(function() {
$("#btn").click(
function(){
sendAjaxForm('result_form', 'ajax_form', 'ajax.php');
return false;
}
);
});
function sendAjaxForm(result_form, ajax_form, url) {
jQuery.ajax({
url: url,
type: "POST",
dataType: "html",
data: jQuery("#"+ajax_form).serialize(),
success: function(response) {
result = jQuery.parseJSON(response);
window.location="http://www.tutorialspoint.com/index.php?id=".result.name;
},
error: function(response) {
document.getElementById(result_form).innerHTML = "Error.";
}
});
}
</script>
window.location="http://www.tutorialspoint.com/index.php?id=".result.name;
на window.location="http://www.tutorialspoint.com/index.php?id="+result+name;
$(function() {
var $btn = $('#btn'),
$form = $('#ajax_form'),
$resultForm = $('#result_form');
$btn.on('click', function() {
console.log('click');
var formData = $form.serialize();
sendAjaxForm(formData);
});
});
function getLink(id) {
return 'http://www.tutorialspoint.com/index.php?id=' + id;
}
function _success(res) {
var data = $.parseJSON(res);
console.log(data);
return window.location = getLink(data.name);
}
function _error(res) {
return $resultForm.html('Error.');
}
function sendAjaxForm(data) {
$.ajax({
url: '/ajax.php',
type: 'POST',
dataType: 'json',
data: data,
success: _success,
error: _error
});
return false;
}