heron
heron

Reputation: 3661

PHP response to ajax

My PHP side sends reponse to ajax like that

$data = array(
            'status' => $status,
            'message' => $message
    );
    echo json_encode($data);

My ajax looks like that

$.ajax({
               url: formUrl,
               type: formMethod,
               dataType: "json",
               data: formData,
               success: function (data) {
                  //setup variables
                  var responseData = jQuery.parseJSON(data), cl, text;

                  //response conditional
                  switch (responseData.status) {
                  case 'error':
                     cl = 'error';
                     text = responseData.message;
                     break;
                  case 'success':
                     cl = 'success';
                     text = 'Qeydiyyat uğurla başa çatdı';
                     break;
                  }

                  $.notifyBar({
                     cls: cl,
                     html: text
                  });

               }
            });

Getting responseData is null error message. But (from firebug XHR) I see that php actually echoes result. What could be the reason?

Upvotes: 2

Views: 271

Answers (1)

nnnnnn
nnnnnn

Reputation: 150010

I believe jQuery is smart enough to parse the response JSON for you such that the data parameter passed to your callback has already been parsed. So you can just access data.status, etc. directly.

Upvotes: 2

Related Questions