jQuery到PHP数据传输

拥有以下jQuery文件

$( document ).ready(function() { var urlform = "register/ajax"; $("#btnRegister").click( function() { $("#Register").submit( function() { return false; }); $.ajax({ url: urlform, type: 'POST', dataType: 'json', async: true, data: $("#Register").serialize(), success: function (data) { $("#email").css("border-color", "green"); $("#rCheck").text(data); console.log(data); }, error: function (data) { $("#email").css("border-color", "red"); $("#rCheck").text(data); console.log(data); } }); }); }); 

在控制台日志中我得到

 Object {email_reg: Object, confirm_password_reg: Object} confirm_password_reg: Object notSame: "Passwords Doesn't Match" __proto__: Object email_reg: Object recordFound: "E-mail address already exists" __proto__: Object __proto__: Object 

有关更清晰的解释,有以下链接:

但是当使用$(“#rCheck”)。文本(数据)在div中传输数据时; 它只获得[object Object]

问题如何仅传输错误消息,如:

  • 密码不匹配
  • 电子邮件地址已存在

Json编码

  if (!$form->isValid()){ // email is invalid; print the reasons $json= $form->getMessages(); $response->setContent(\Zend\Json\Json::encode($json)); } return $response; 

怎么在这里$ json可以在数组中双关语。

问题是data是一个对象,当传递给.text()会打印它的.toString()版本,即[object Object]

所以试试吧

 if (data) { var msgs = $.map(data, function (fieldObj, key) { return [$.map(fieldObj, function (msg, key) { return msg; })] }); $('#abc').html(msgs.join('
')) } else { //do something }