/    Sign up×
Community /Pin to ProfileBookmark

Posting Data Getting JSON Back

I’m posting data via fetch to a page, it’s doing something with my database and I want to return a JSON message.

Here’s what I’ve got

My Post

“`
fetch(“../process/do_something_.php”,
{
body: formData,
method: “post”
})
.then(response => {
response.json()
})
.then(data => console.log(data));
}
“`

do_something.php

“`
header(‘Content-type: application/json’);
if ($error === 0){
$array = array(‘user’ => $ID, ‘outcome’ => true);
echo json_encode($array);

} else {
$array = array(‘user’ => 0, ‘outcome’ => false);
echo json_encode($array);
}
“`

I’m constantly getting

“VM162:1 Uncaught (in promise) SyntaxError: Unexpected end of JSON input”

What am I doing wrong?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 07.2022 — Obviously the text your PHP script is returning is no valid JSON .

Take a look at the network tab in the developer tools of your browser to view what the script is returning.
Copy linkTweet thisAlerts:
@NogDogMay 07.2022 — Try calling the php script directly from your browser, then see what it returns. Maybe there's a warning that headers were already sent or such?
Copy linkTweet thisAlerts:
@VictorJohnsonandroniMay 10.2022 — where do i edit the header? i've got an ASPX page, and let's say i need to show the authToken (returned from the post) in a label on that page, how do i do that?
Copy linkTweet thisAlerts:
@NogDogMay 10.2022 — @VictorJohnsonandroni#1643928

Sounds like a separate issue that should be in its own thread, with appropriate tags?
Copy linkTweet thisAlerts:
@billyrobertsMay 11.2022 — Your PHP script is not returning valid JSON. Check the network tab in your browser's developer tools to see what the script is actually returning.

**Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.**
Copy linkTweet thisAlerts:
@easollJun 24.2022 — Custom paper. Have you tried to trust your [personal statement help online](https://www.bestcustomwriting.com/personal-statement-help) to someone experienced and get a perfect one? If not, you aren't a college student yet; otherwise, it would be impossible for you to get through 1 semester without ordering at least one custom paper. Although many diligent students and their professors are against any kind of help, all these stereotypes and opinions aren't supported by any good evidence. You hardly want to get anxiety or depression because of exhausting assignments, so why not try to order custom papers?
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...