Function doesn't work after php response
I have an ajax request sending some data to a php file:
Ext.Ajax.request({
url: 'update.php',
method: 'POST',
success: function (){ alert("saved"); },
failure: function (){ alert("not saved"); },
params: { data: someData }
})
and update.php that has only two lines:
<?php
$res = array('success' => false);
echo json_encode($re开发者_高级运维s);
?>
The success function is always executed. What can be the problem?
For me, the function failure should be called when there is a problem with the request. e.g. If the server is down or the page update.php do not exist anymore.
Ext.Ajax.success
is executed because the AJAX request was successful. If you rename or delete update.php, it'll execute .failure
because it's getting a 404.
You have to check the response in .success
or make update.php return a 404.
success
is called when the request succeeded. The data you reply with is irrelevant.
The error
callback is invoked when the request failed. It's not failing, so the callback is not being invoked.
Parse the resultant data within the success callback to look at application-level logic:
Ext.Ajax.request({
url: 'update.php',
method: 'POST',
success: function (data){ alert(data.success ? "saved" : "not saved"); },
failure: function (){ alert("request failed"); },
params: { data: someData }
});
I have assumed that success
may take a parameter with the response contents; check the documentation for Ext to find out.
精彩评论