0

Receiving JSON data using $.post .... and i'm trying to add an if statement when it's null. Looked at following links:

But in Google Chrome it appears errors like Uncaught TypeError: undefined is not a function .

Detailed Problem issue

I'm using Zend Framework and in LoginController.php have

public function ajaxAction()
{
    $form = $this->getServiceLocator()->get('LoginForm');
    $post = $this->request->getPost();
    $form->setData($post);
    $response   = $this->getResponse();
    if (!$form->isValid()){
        // something is invalid; print the reasons
        $json= $form->getMessages();
        $response->setContent(\Zend\Json\Json::encode($json));
        return $response;
    } else{
        $this->getAuthService()->getAdapter()->setIdentity(
            $this->request->getPost('email'))->setCredential(
            $this->request->getPost('password'));
        $result = $this->getAuthService()->authenticate();
        switch ($result->getCode()) {
            case Result::FAILURE_IDENTITY_NOT_FOUND:
            $json = "Email or password is incorrect";
            $response->setContent(\Zend\Json\Json::encode($json));
            return $response;
            case Result::FAILURE_CREDENTIAL_INVALID:
            $json = "Email or password is incorrect";
            $response->setContent(\Zend\Json\Json::encode($json));
            return $response;
        }
    }
}

This Action getError messages and in script.js i echo them to view.

....

    $.post(url,data,function(resp)
    {
        if (typeof resp === "object") {
            $('#'+id).parent().find('.alert-danger').remove();
            $('#'+id).parent().append(getErrorHtml(resp[id], id));
            console.log(typeof resp);
          } else {
            $("#formLoginErrorMessage").addClass("alert-danger");
            $("#formLoginErrorMessage").append("<li>" + resp + "</li>");
            console.log(typeof resp);
          }
    },'json');
....

Where resp contains error Messages. I want to check if resp is null then add something like:

if(resp isNull)
{
    ('#loginForm').submit(function(){
     return true;
    });
 } else {
    ('#loginForm').submit(function(){
     return false;
    });
     $.post(......
 }

Hope this will clear things a little bit.

500 error When there are no error messages in Chrome Developer Tools appears error:

POST http://new.local/register/ajax/ 500 (Internal Server Error) jquery-1.8.2.min.js:2

send jquery-1.8.2.min.js:2

p.extend.ajax jquery-1.8.2.min.js:2

p.(anonymous function) jquery-1.8.2.min.js:2

doValidation script.js:26

(anonymous function) script.js:9

p.event.dispatch jquery-1.8.2.min.js:2

g.handle.h

Community
  • 1
  • 1
I M
  • 61
  • 1
  • 9

1 Answers1

0

$.getJSON expects the response to be valid JSON. If it might be empty, that's not valid, so you can't use this function. You should use $.get() and call $.parseJSON() after checking that the response is not empty:

$.get(URL, function(json) {
    if (json != '') {
        data = $.parseJSON(json);
    } else {
        // do something else
    }
});
Barmar
  • 741,623
  • 53
  • 500
  • 612