jQuery Ajax获取json返回的结果

时间:2018-06-05 13:40:40

标签: php jquery json ajax

我想在Ajax请求中获取php函数的返回结果,以便在onSucces中进行一些验证。如何从php函数中获取JSON结果到ajax请求中?

public function verifyEmailAction()
{
    $is_valid = true;
    $is_duplicate = false;
    $email_reset = false;

    $register = $this->getRegisterHelper();
    $register->addEmailCheck();
    $register->setData($this->getAllParams());
    $validationErr = $register->getValidationResult();

    if (!empty($validationErr['badWords']['email']) || $validationErr['banned']['email'] == true
        || empty($validationErr['isEmailValid'])
    ) {
        $is_valid = false;
    } elseif (!empty($validationErr['duplicates']['email'])) {
        $is_duplicate = true;
        $email_reset = $this->sendResetEmail();
    }
    $result = [
      'duplicate' => $is_duplicate,
      'valid' => $is_valid,
      'reset' => $email_reset
    ];

    $this->getResponse()->setBody(json_encode($result));
}




jQuery.validator.addMethod("checkDuplicate", function (value, element) {
        jQuery.ajax({
            type: 'GET',
            url: '/user/register/verify-email.ajax',
            data: {
                'email': value
            }
        });
    });

5 个答案:

答案 0 :(得分:0)

 jQuery.ajax({
            type: 'GET',
            url: '/user/register/verify-email.ajax',
            data: {
                'email': value
            },
            dataType:'json',
            success:function(response){
               console.log(response);
               var duplicate=response.duplicate;
               var valid=response.valid;
               var reset=response.reset;
            },
            error:function(err){
               console.log('Error '+err);
            }
        });

答案 1 :(得分:0)

您需要使用如下所示的成功和错误功能

jQuery.validator.addMethod("checkDuplicate", function (value, element) {
        jQuery.ajax({
            type: 'GET',
            url: '/user/register/verify-email.ajax',
            data: {
                'email': value
            },
            success : function(data, status, xhr) {
                console.log(JSON.stringify(data));
            },
            error: function(jqXhr, textStatus, errorMessage){
                  console.log("ERROR " + errorMessage);
            }
        });
    });

答案 2 :(得分:0)

$.ajax({
  type: 'GET',
  url: url,
  data: {
   'email': value
  },
  dataType:'json',
  }).success(function(response){ 
        //YOUR Json KEY
        if(response.success){
        }            
});

答案 3 :(得分:0)

我希望这篇文章可以帮到你。 http://api.jquery.com/jquery.ajax/

特别是你可以使用这个

jQuery.ajax({
  url: "YOURURL",
  type: "YOURTYPE",
  dataType:"json",
}).done(function(data) {
  console.log(data) // to see the reqested data
  // manipulate data here
});

答案 4 :(得分:0)

添加dataType:'json'

jQuery.validator.addMethod("checkDuplicate", function (value, element) {
        jQuery.ajax({
            type: 'GET',
            url: '/user/register/verify-email.ajax',
            data: {
                'email': value
            },
            dataType:'json'
        });
    });

您可以使用它将JSON字符串转换为JavaScript对象:

var txtReturned = JSON.parse(data);

参考: