1

I have some ajax call like this

function ExportData() {
  var data = {
    action: "export_database", // the name of your PHP function!
  };

  jQuery.ajax({
    type: "POST",
    url: ajaxurl,
    data: data,
    beforeSend: function () {},
    success: function (data) {
      alert(data);
    },
  });
}

And php function like this

function export_database(){
  return $response;
}

The problem is in that response I have something like this

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|3fa58ee1-48bf0cb9f60bfa25."
}

I want to alert only title, but when i try data.title , i got undefine Do I must encode or decode something, thanks?

6
  • 1
    while returning from export_database function, return like json_encode($response). and then you can use JSON.parse(data) in JS. Commented Jun 18, 2020 at 6:47
  • Maybe you need to parse JSON? Using JSON.parse(response). Commented Jun 18, 2020 at 6:47
  • see the updated answer. Let me know. Commented Jun 18, 2020 at 6:53
  • @MiomirDancevic did u try doing what I suggested? Commented Jun 18, 2020 at 6:59
  • @RohitAmbre i do not think there is a need on the php side to do json_encode as the response he is getting is already an json object. Commented Jun 18, 2020 at 7:03

3 Answers 3

1

This is what you need. Just access the object by data.title and it will show in the alert()

You need to define dataType as json in your request.

If its does not work then use JSON.parse(data) like this:

var response = JSON.parse(data)
alert(response.title)

Try below:

function ExportData() {
  var data = {
    action: "export_database", // the name of your PHP function!
  };

  jQuery.ajax({
    type: "POST",
    url: ajaxurl,
    dataType: 'json'
    data: data,
    beforeSend: function () {},
    success: function (data) {
      alert(data.title);
    },
    error: function(error){
      //Error
      alert(error.title)
    }
  });
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried alert(data.title); but i got undefined??
1

Try Below:

    function ExportData() {
      var data = {
        action: "export_database", // the name of your PHP function!
      };

      jQuery.ajax({
        type: "POST",
        url: ajaxurl,
        data: data,
        beforeSend: function () {},
        success: function (data) {
          var parsedData = jQuery.parseJSON(data)
          alert(parsedData.title);
        },
      });
    }

Comments

0

You have to use JSON.parse() for accessing data objects Like this:

function ExportData() {
  var data = {
    action: "export_database", // the name of your PHP function!
  };

  jQuery.ajax({
    type: "POST",
    url: ajaxurl,
    data: data,
    beforeSend: function () {},
    success: function (data) {
var res = JSON.parse(data)
      alert(res.title);
    },
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.