leviboy
leviboy

Reputation: 11

HTML is being appended at the end of JSON data, how can i separate them?

I'm submitting values of a form to another file that processes them but that file also has HTML code in it. I'm echoing json_encode() on that file in order to return the response but the HTML gets appended at the end of the JSON data like this:

"{"firstname":"levi","secondname":"johnson"}<!DOCTYPE html><html><head>"

i want them separate as its messing up the JSON data i want to return. when i remove the HTML part of the file it works fine. I have tried using a content-type header on the php part but it doesn't help.

Upvotes: 0

Views: 129

Answers (2)

Pupil
Pupil

Reputation: 23978

In the AJAX backend file, send some parameter like from where the request came.

If its JSON, then put proper json header.

And exit there.

Else, show plain HTML.

This way, your file should also work for plain HTML along with this JSON request.

Just writing exit in your code will break your functionality where this file's HTML is required.

Upvotes: 1

Xmindz
Xmindz

Reputation: 1282

Try ending the response from the server code by putting an exit; right after returning the json.

Upvotes: 4

Related Questions