用ajax回答错误请求时读取json结果

I'd like to send an error to the user via ajax, if he don't fill the form properly.

This is how I send the answer to the browser via ajax:

if($bo){
    header('HTTP/1.1 400 Bad Request');
    header('Content-Type: application/json; charset=UTF-8');
    $error['message'] = 'Some error message';
    echo json_encode($error);
    die;
}

And this is my jquery script:

$.ajax(
            {
                url: formURL,
                type: "POST",
                data: postData,
                success: function(data, textStatus, jqXHR)
                {

                    // some code ...

                },
                error: function(data, jqXHR, textStatus, errorThrown)
                {
                    // how to get message here json{"message":"some_message"}
                    var jsonObj = JSON.parse(data);
                    $r = jsonObj.message;
                    alert($r);                    
               }
            });

I can see the message using firebug console but I don't know how to get the variable containing my error :/

Any suggestion please?

Thanks

Thanks everybody.

I guess I've found the solution. To access message value:

error: function(data, jqXHR, textStatus, errorThrown) 
        {
            console.log(data.responseJSON.message);
        }

First, I suggest using return instead of echo & die. It is more neat and gives closure.

Second, There is no need to parse the json in javascript, it's like chocolate, it simply understands. Just use accept: application/json or jsonp in your ajax input object and use data.message.

These two tricks combined should solve your problem.