Php yui3 io如何返回失败或成功?

Php yui3 io如何返回失败或成功?,php,javascript,yui3,Php,Javascript,Yui3,我有以下资料: YUI().use("io-form", function(Y) { var cfg = { method: 'POST', form: { id: 'subscribe-form', useDisabled: false } }; function login() {

我有以下资料:

YUI().use("io-form",
    function(Y) {
        var cfg = {
            method: 'POST',
            form: {
                id: 'subscribe-form',
                useDisabled: false
            }
        };
        function login() {
            Y.io('process.php', cfg);
            Y.on('io:success', onSuccess, this);
            Y.on('io:failure', onFailure, this);
        };
        function onSuccess(id,response,args) {
            document.getElementById('myformmsg').innerHTML = response.responseText;
            document.forms['myform'].reset();
        };
        function onFailure(id,response,args) {
            document.getElementById('myformmsg').innerHTML = "Error, retry...";
            document.forms['myform'].reset();
        };
        Y.on('click', login, '#myformbutton', this, true);
});

你怎么知道是否会失败。我必须从PHP返回什么?

这取决于返回的http状态代码的标题。 假设状态代码为200,它将转到onSuccess。 假设状态代码为500(内部服务器错误),它将转到onFailure

此处列出HTTP状态代码:

如果php中有一些致命错误,它仍然会返回状态200,因为请求成功

如果您想处理php错误,我建议您像每次成功时一样使用json返回:

{
  status: 0, // Let say 0 for OK, -1 for Error, you can define more by yourself
  results: <anything you want here>,
  errors: <errors message/errors code for your ajax handler to handle>
}
在javascript中,您将如下处理:

$response = array(
    'status' => 0,
    'results' => 'something good ...',
    'errors' => 'error message if status is -1'
);
echo json_encode($response);
function onSuccess(id,response,args) {
     var responseObj = Y.JSON.parse(response);

     if (responseObj.status === 0) { 
         // Request and process by php successful
     }
     else {
         // Error handling
         alert(responseObj.errors);
     }
};
请记住,如果要使用Y.JSON,则需要包含“JSON解析”,例如:

YUI().use('json-parse', , function (Y) {
    // JSON is available and ready for use. Add implementation
    // code here.
});

是否有人有一个简洁的方法来传回一个声明错误的数组?