1

I want to get the return result of a php function in an Ajax request in order to make some verification in onSucces. How can I get the JSON result from the php function into the ajax request?

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
            }
        });
    });
Alex
  • 11
  • 3
  • Something like this .success(function(response){ // here is your response }) – DsRaj Jun 05 '18 at 13:43
  • 2
    Possible duplicate of [How to submit form with Ajax and Jquery Validation?](https://stackoverflow.com/questions/27936749/how-to-submit-form-with-ajax-and-jquery-validation) – freedomn-m Jun 05 '18 at 13:45

5 Answers5

1
 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);
            }
        });
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

You need to use the success and error functions like below,

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);
            }
        });
    });
Ajanth
  • 2,435
  • 3
  • 20
  • 23
0
$.ajax({
  type: 'GET',
  url: url,
  data: {
   'email': value
  },
  dataType:'json',
  }).success(function(response){ 
        //YOUR Json KEY
        if(response.success){
        }            
});
DsRaj
  • 2,288
  • 1
  • 16
  • 26
0

I hope this article will help you. http://api.jquery.com/jquery.ajax/

Specially you can use this

jQuery.ajax({
  url: "YOURURL",
  type: "YOURTYPE",
  dataType:"json",
}).done(function(data) {
  console.log(data) // to see the reqested data
  // manipulate data here
});
Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
0

Add dataType:'json':

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

You can use this to convert JSON string to a JavaScript object:

var txtReturned = JSON.parse(data);

Reference:

Community
  • 1
  • 1
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58