好吧,我有一个表单,类似于dajax“django forms”示例,除了返回的JSON没有被处理外,一切正常,我修改了jquery.dajax.core.js,如下所示:
...
process: function(data)
{
if(data==Dajaxice.EXCEPTION){
alert('Something went wrong, please reload the page.');
}
else{
console.log(data);
alert("ENTRO!");
$.each(data, function(i,elem){
alert("ENTRO!2");
console.log(elem.cmd);
switch(elem.cmd)
{
case 'alert':
alert(elem.val)
break;
...
我从ajax.py返回的一个json是:
[{"cmd": "remcc", "id": "#contact_form", "val": ["error"]}, {"cmd": "alert", "val": "Thank you, Alejandro"}, {"cmd": "js", "val": "alert(\"jummmmm\");"}]
当json在浏览器中被处理回来时,我得到第一个“Entro!”,但没有别的,没有处理JSON。 如果故意破坏ajax.py函数中的东西,我会得到正确的行为,警告Dajaxice.EXCEPTION。
有什么建议吗?
- 编辑 -
Dajax.process函数:
validateContactForm = function() {
datos = jQuery('#contact_form').serializeObject();
console.log(datos);
// jQuery
// If you are using jQuery, you need this form->object serializer
// https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-serializeobject.js
Dajaxice.ajax.validateContactForm(Dajax.process,{'formulario':datos});
}
和ajax.py函数:
from dajax.core import Dajax
@dajaxice_register
def validateContactForm(request, formulario):
dajax = Dajax()
form = ContactoForm(formulario)
if form.is_valid():
dajax.remove_css_class('#contact_form','error')
dajax.alert(_("Thank you, %(name)s") % {"name":form.cleaned_data.get('nombre')})
form.save();
dajax.script('alert("jummmmm");')
print dajax, dir(dajax)
else:
dajax.remove_css_class('#contact_form','error')
print form.as_ul()
for error in form.errors:
dajax.add_css_class('#id_%s' % error,'error')
print error
print dajax, dir(dajax)
return dajax.json()
好吧,Firebug失败了,chrome说:
Uncaught TypeError: Cannot call method 'each' of undefined
Dajax.process
oXMLHttpRequest.onreadystatechange
b.dispatchEventda
k
b.open._object.onreadystatechange
和Opera的蜻蜓:
Uncaught TypeError: Cannot call method 'each' of undefined
Dajax.processjquery.dajax.core.js:11
oXMLHttpRequest.onreadystatechangedajaxice.core.js:100
b.dispatchEventdajaxice.core.js:141
kdajaxice.core.js:135
b.open._object.onreadystatechange
使用相同行为测试的jQuery版本:
1.6.2 1.6.2.min 1.6.4