-2

This does not answer the question - jQuery Ajax return html AND json data

I can not use the wrap method because I do not have a single html string. I am not using Phery, I have tried the first option here and the second nested call runs before the first so that data is undefined- jquery Use two done callbacks in same function, one with datatype json one without and we are not using html5 only for this. Therefore the referenced answer does not address the question.

In my .done function I have (data) returning. This data is a mixture of php echo's and a single JSON_encode array. Sample data:

.....
//other divs and data above this point
<div id="test">text</div>
{"ids":["1","5","2","6"]}0

AJAX

.done(function(data) {
console.log(data);
func2(json_string)
})

I want to be able to extract and separately work with the array {"ids":["1","5","2","6"]}0. I am trying to pass it along to another function, but only that portion of the data

9
  • 2
    Can you not change the PHP script to respond with something more usable? Commented Feb 1, 2021 at 2:37
  • What would be more usable? I just cut off the other divs coming back before showing the array I was looking for help with Commented Feb 1, 2021 at 2:38
  • For example, use the Accept request header to decide whether to respond with HTML or JSON (but not both) Commented Feb 1, 2021 at 2:39
  • 1
    more useful <--- two alternatives. 1. Put the view partial (the HTML bit) in a string and include that in the encoded object with some key that makes sense to you. 2. Redesign so the view partial is returned from one request and the data from a second request. Commented Feb 1, 2021 at 2:39
  • 1
    Perhaps you can use a combination of Javasript indexOf() and substr() methods to detect the opening and closing braces of the JSON string and extract that out? Commented Feb 1, 2021 at 4:02

1 Answer 1

1

Here's a very simple content-negotiation example using a query string parameter...

In your PHP, break up your HTML and JSON parts like this

<?php
$format = $_GET["format"] ?? "html";
if ($format === "json") {
    header("Content-type: application/json");
    echo json_encode(["ids" => [1, 5, 2, 6]]);
    exit;
}
?>
.....
//other divs and data above this point
<div id="test">text</div>

Then in your JavaScript code, fetch the two parts using

$.ajax(my_ajax.ajax_url, {
  dataType: "html",
  // etc
}).done(html => {
  // html content here
})

and

$.ajax(`${my_ajax.ajax_url}?format=json`, {
  dataType: "json",
  // etc
}).done(json => {
  // json content here
})
Sign up to request clarification or add additional context in comments.

2 Comments

Would this also work if the php cant be broken up because the ids are coming from a foeach loop that is also used to generate some of the html?
I don't know what you mean since I cannot see your PHP code. If this hasn't answered your question, why did you accept it?

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.