1

I have an array like this in my PHP page named new1.php:

$arr = ['value 1', 'value 2', 'value 3'];
$html = '<div>huge data with all tags like a page</div>';
$response = json_encode('array' => $arr, 'html' => $html);
echo $response

In the calling page, when I console.log(data.html) it gives undefined. The same happens for console.log(data.array);. Here is my AJAX code:

$.ajax({
    url: "new1.php",
    type: "POST", 
    data: { somedata: somedata },
    dataType: "text",
    success: function(data) {
        console.log(data);
        console.log(data.html);
        console.log(data.array);
    }
});

Most importantly, I want to know what is the best way to return a page with other data from AJAX response?

7
  • 6
    dataType:"text", change to dataType:"json", Commented Nov 10, 2016 at 10:28
  • @JonStirling, there is no syntax error Commented Nov 10, 2016 at 10:29
  • @guradio, if i do so will i be able to get html page or html content Commented Nov 10, 2016 at 10:30
  • 2
    @user5405873 There sure as hell was before you updated the question. We can all see the question history... Commented Nov 10, 2016 at 10:30
  • I take it back, there are still syntax errors. See json_encode('array' => $arr, 'html' => $html);. Commented Nov 10, 2016 at 10:38

2 Answers 2

2

from your php code where you do json_encode add this to the top of the page header("Content-Type: application/json"); then your encode should take in array as parameter instead

json_encode(array("array"=>$arr, "html"=>$html));

it should see your record as json now and please change ur dataType to json from Jquery intelligence guess from the server state (jquery) it will automatically take json instead

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

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

7 Comments

if i do so can i be able to get html page or html content?
yes, with the data.* format of any content , or now `console.log('my data', data) , it will be logged as json object where you can see the children and their content
so you dont need to do parse json, when the server is already coming as json already like a .json file. just keep me posted
i have tried your method but same error <b>Parse error</b>: syntax error, unexpected '=&gt;
thanks alot man for your patience and being with me from start
|
2

You should be json parse, because you are json encoding from php file, and as there is data type of your ajax is text so you need to parse the json.

 $.ajax({
         url:"new1.php",
         type:"POST",
         data:{somedata:somedata},
         dataType:"text",
         success: function(data){
                data = JSON.parse(data);
                  console.log(data);
                console.log(data.html);
                console.log(data.array);

            }
    });

5 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
it is giving error like <b>Parse error</b>: syntax error, unexpected '=&gt;' (T_DOUBLE_ARROW) in
are you putting exit after : echo $response ? in php file
Try by replace php file like : $arr = ['value 1', 'value 2', 'value 3']; $html = '<div>huge data with all tags like a page</div>'; $response = json_encode('array' => $arr, 'html' => $html); echo $response exit;
I've got a bad feeling about this. Seems like one of those questions where you answer the simple, initial question, and then spend hours answering "but what about..." questions. Good luck!

Your Answer

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