I try a lot of ways to prevent this, but nothing works which is puzzling.
This is the route for get.
router.get('/some', function(request, response, next) {
console.log('> info: some');
response.send({"hello": "world"});
}
This is the AJAX part hooked to onClick
of an element.
on_click = function(event) {
//console.log(event.href);
event = event || window.event;
var target = event.target || event.srcElement;
if (target.nodeType == 3)
target = target.parentNode;
target.preventDefault();
target.stopPropagation();
target.stopImmediatePropagation();
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
$.ajax({
type: 'GET',
url: target.href,
dataType: 'json',
cache: false
})
.done(function(received_data) {
$('.container').html('<h1>hello here</h1>');
return false;
})
.fail(function(xhr, status, error) {
alert(xhr.responseText);
return false;
});
return false;
}
I expect to see "hello here"
. It works for a small test program. But when I add to my development code, it always show a white page with the JSON string of hello world.
So far I have tried the followings.
- send string instead of JSON object directly
- change
dataType
totext
- set
processData
tofalse
preventDefault
,stopPropagation
,stopImmediatePropagation
I apology that I cannot upload the development code, but any guess why it is so stubborn to refresh the page?
BTW, for POST, it works perfectly.
This is the related element:
<a href="/some" onClick="on_click(this); return false;"></a>