2011-08-21 69 views
0

我正在使用Ruby on Rails v3.0.9和jQuery v1.6.1。当AJAX HTTP请求返回error(请参阅下面的代码以获取更多信息)以便显示@user.errors消息时,我想处理(通过使用jQuery)JSON响应数据。也就是说,...如何在AJAX HTTP请求返回`error`时处理`@ user.errors` JSON数据?

...控制器我有:

render :json => @user.errors, 
     :status => 400 

...在视图文件我有:

$jQ.ajax({ 
    type:  '...', 
    url:  '...', 
    dataType: 'json', 
    data:  '...', 
    error: function(jqXHR, textStatus, errorThrown) { 

    // Here is where I would like to handle the above mentioned `@user.errors` 
    // array (that is related to the "Ruby on Rails Way" of handling errors). 
    // 
    // I tried to use 'alert(jqXHR.base);' but the `@user.errors` array is always 
    // returned as a string. 
    alert(...) 

    }, 
    success: function(data, textStatus, jqXHR) { 
    ... 
    } 
}); 

有改变的方式上面的代码,以便使用类似以下内容来显示错误?

# Supposing that'@user.errors' is '{"base":["already exist"]}' 
alert(jqXHR.base); 

注:在几句话,我想处理数据的取得时,我得到一个成功的AJAX HTTP请求数据(function(data, textStatus, jqXHR)的允许我使用JSON解析data像这样:alert(jqXHR.base);)。

回答