Jquery Django API如何将验证消息返回给用户
我正在使用TastyPie注册一个新用户。我希望在警报框中向用户显示所有验证消息。我注意到TastyPie给了我以下信息:Jquery Django API如何将验证消息返回给用户,jquery,python,ajax,django,json,Jquery,Python,Ajax,Django,Json,我正在使用TastyPie注册一个新用户。我希望在警报框中向用户显示所有验证消息。我注意到TastyPie给了我以下信息:responseJSON和responseText responseJSON Object { accounts/create={...}} accounts/create Object { email=[1], password2=[1]} responseText "{"accounts/create": {"email": ["This field
responseJSON
和responseText
responseJSON Object { accounts/create={...}}
accounts/create
Object { email=[1], password2=[1]}
responseText
"{"accounts/create": {"email": ["This field is required."], "password2": ["This field is required."]}}"
如何向用户显示验证并正确解析?TastyPie是否有任何内置函数来解析错误
这是我到目前为止遇到的运行错误,但我没有收到任何错误消息:
error: function(errorThrown){
data = JSON.parse(errorThrown.responseText);
alert(data.error)
console.log(errorThrown);
}
});
尝试以下方法:
var jsonString = '{"accounts/create": {"email": ["This field is required."], "password2": ["This field is required."]}}';
var data = JSON.parse(jsonString);
var inputsInError = (data["accounts/create"]);
for (var key in inputsInError) {
if (inputsInError.hasOwnProperty(key)) {
alert(key + " -> " + inputsInError[key]);
}
}
请参阅jsiddle:尝试以下方法:
var jsonString = '{"accounts/create": {"email": ["This field is required."], "password2": ["This field is required."]}}';
var data = JSON.parse(jsonString);
var inputsInError = (data["accounts/create"]);
for (var key in inputsInError) {
if (inputsInError.hasOwnProperty(key)) {
alert(key + " -> " + inputsInError[key]);
}
}
请参阅jsiddle:尝试以下方法:
var jsonString = '{"accounts/create": {"email": ["This field is required."], "password2": ["This field is required."]}}';
var data = JSON.parse(jsonString);
var inputsInError = (data["accounts/create"]);
for (var key in inputsInError) {
if (inputsInError.hasOwnProperty(key)) {
alert(key + " -> " + inputsInError[key]);
}
}
请参阅jsiddle:尝试以下方法:
var jsonString = '{"accounts/create": {"email": ["This field is required."], "password2": ["This field is required."]}}';
var data = JSON.parse(jsonString);
var inputsInError = (data["accounts/create"]);
for (var key in inputsInError) {
if (inputsInError.hasOwnProperty(key)) {
alert(key + " -> " + inputsInError[key]);
}
}
请参阅JSFIDLE:您的错误似乎在jQuery方面。错误键指向不同定义的函数(请参阅) 错误 类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN) 请求失败时要调用的函数。这个 函数接收三个参数:jqXHR(在jquery1.4.x中, XMLHttpRequest)对象,一个描述 如果发生异常,则为可选的异常对象。可能的 第二个参数的值(除null外)为“超时”、“错误”, “中止”和“解析错误”。当HTTP错误发生时,会抛出错误 接收HTTP状态的文本部分,例如“未找到” 或“内部服务器错误”。从jQuery 1.5开始,错误设置可以 接受函数数组。每个函数将依次调用。 注意:对于跨域脚本和 跨域JSONP请求。这是一个Ajax事件 所以你的JS代码应该是
error: function(jqXHR, textStatus, errorThrown) {
...
},
但实际上,当您从django返回数据时,这是一个成功。jQuery中的“错误”是指通信错误,而不是语义错误
如果你只使用
error: function(jqXHR, textStatus, errorThrown) {
alert("An error!")
},
您可能会看到它没有被调用(您需要从django返回400/500错误代码才能调用它)
因此,您可以做的是区分在“真正”成功的情况下成功返回的内容和在服务器端失败的情况下成功返回的内容:
# and/or answer.data['errorMessage'] = "Error type II"
answer.data['success'] = False
return answer
然后在jQuery中:
success: function(data, textStatus, jqXHR) {
// instead of checking for success, you can check the content of
// the "accounts/create" key
// if (!data.success) {
// I assume that this key won't be present in case of error.
if (data["accounts/create"]) {
alert("Errors will now be displayed")
// ...here, the same code in Corinne Kubler's answer; or you
// can make it more jQueryish with e.g. .each()
return;
}
alert("Everything OK, proceeding")
// ...
}
您的错误似乎在jQuery方面。错误键指向不同定义的函数(请参阅)
错误
类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN)
请求失败时要调用的函数。这个
函数接收三个参数:jqXHR(在jquery1.4.x中,
XMLHttpRequest)对象,一个描述
如果发生异常,则为可选的异常对象。可能的
第二个参数的值(除null外)为“超时”、“错误”,
“中止”和“解析错误”。当HTTP错误发生时,会抛出错误
接收HTTP状态的文本部分,例如“未找到”
或“内部服务器错误”。从jQuery 1.5开始,错误设置可以
接受函数数组。每个函数将依次调用。
注意:对于跨域脚本和
跨域JSONP请求。这是一个Ajax事件
所以你的JS代码应该是
error: function(jqXHR, textStatus, errorThrown) {
...
},
但实际上,当您从django返回数据时,这是一个成功。jQuery中的“错误”是指通信错误,而不是语义错误
如果你只使用
error: function(jqXHR, textStatus, errorThrown) {
alert("An error!")
},
您可能会看到它没有被调用(您需要从django返回400/500错误代码才能调用它)
因此,您可以做的是区分在“真正”成功的情况下成功返回的内容和在服务器端失败的情况下成功返回的内容:
# and/or answer.data['errorMessage'] = "Error type II"
answer.data['success'] = False
return answer
然后在jQuery中:
success: function(data, textStatus, jqXHR) {
// instead of checking for success, you can check the content of
// the "accounts/create" key
// if (!data.success) {
// I assume that this key won't be present in case of error.
if (data["accounts/create"]) {
alert("Errors will now be displayed")
// ...here, the same code in Corinne Kubler's answer; or you
// can make it more jQueryish with e.g. .each()
return;
}
alert("Everything OK, proceeding")
// ...
}
您的错误似乎在jQuery方面。错误键指向不同定义的函数(请参阅)
错误
类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN)
请求失败时要调用的函数。这个
函数接收三个参数:jqXHR(在jquery1.4.x中,
XMLHttpRequest)对象,一个描述
如果发生异常,则为可选的异常对象。可能的
第二个参数的值(除null外)为“超时”、“错误”,
“中止”和“解析错误”。当HTTP错误发生时,会抛出错误
接收HTTP状态的文本部分,例如“未找到”
或“内部服务器错误”。从jQuery 1.5开始,错误设置可以
接受函数数组。每个函数将依次调用。
注意:对于跨域脚本和
跨域JSONP请求。这是一个Ajax事件
所以你的JS代码应该是
error: function(jqXHR, textStatus, errorThrown) {
...
},
但实际上,当您从django返回数据时,这是一个成功。jQuery中的“错误”是指通信错误,而不是语义错误
如果你只使用
error: function(jqXHR, textStatus, errorThrown) {
alert("An error!")
},
您可能会看到它没有被调用(您需要从django返回400/500错误代码才能调用它)
因此,您可以做的是区分在“真正”成功的情况下成功返回的内容和在服务器端失败的情况下成功返回的内容:
# and/or answer.data['errorMessage'] = "Error type II"
answer.data['success'] = False
return answer
然后在jQuery中:
success: function(data, textStatus, jqXHR) {
// instead of checking for success, you can check the content of
// the "accounts/create" key
// if (!data.success) {
// I assume that this key won't be present in case of error.
if (data["accounts/create"]) {
alert("Errors will now be displayed")
// ...here, the same code in Corinne Kubler's answer; or you
// can make it more jQueryish with e.g. .each()
return;
}
alert("Everything OK, proceeding")
// ...
}
您的错误似乎在jQuery方面。错误键指向不同定义的函数(请参阅)
错误
类型:函数(jqXHR jqXHR、字符串textStatus、字符串ERRORSHORN)
请求失败时要调用的函数。这个
函数接收三个参数:jqXHR(在jquery1.4.x中,
XMLHttpRequest)对象,一个描述
如果发生异常,则为可选的异常对象。可能的
第二个参数的值(除null外)为“超时”、“错误”,
“中止”和“解析错误”。当HTTP错误发生时,会抛出错误
接收HTTP状态的文本部分,例如“未找到”
或“内部服务器错误”。从jQu开始