Click here to Skip to main content
15,121,741 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: 8yr JS now giving me console error in FF and CHROME Pin
Member 1514660812-Apr-21 4:14
MemberMember 1514660812-Apr-21 4:14 
GeneralRe: 8yr JS now giving me console error in FF and CHROME Pin
Richard Deeming12-Apr-21 4:39
mveRichard Deeming12-Apr-21 4:39 
GeneralRe: 8yr JS now giving me console error in FF and CHROME Pin
Member 1514660812-Apr-21 5:00
MemberMember 1514660812-Apr-21 5:00 
QuestionKendo UI for jQuery Combobox noDataTemplate Not Loading Pin
Hypermommy5-Apr-21 10:35
MemberHypermommy5-Apr-21 10:35 
Questionquirks with use of Audio() Pin
Derell Licht31-Mar-21 17:03
professionalDerell Licht31-Mar-21 17:03 
QuestionBizarre JSON parse problem Pin
intoittendenz29-Mar-21 0:27
Memberintoittendenz29-Mar-21 0:27 
AnswerRe: Bizarre JSON parse problem Pin
Richard Deeming29-Mar-21 1:36
mveRichard Deeming29-Mar-21 1:36 
QuestionCreating a JSON body payload in plain JavaScript Pin
jkirkerx27-Mar-21 10:46
professionaljkirkerx27-Mar-21 10:46 
I'm SMH on this.

I wrote an API in PHP 7.4, that gets the body in the header, converts it to an object and I do the database write. It works fine in Postman, but my app seems to send garbage to my PHP API. Process of elimination tells me that I didn't package my JSON right in plain JavaScript.
const jsonData = {
            "projectNumber": projectNoElement.value,
            "userName" : userNameElement.value,
            "userType" : userTypeElement.value,
            "invoiceClass": classElement.value,
            "invoiceOperation": opElement.value,
            "subContractor": scElement.value,
            "invoiceDate": dateElement.value,
            "invoiceNumber": invNumberElement.value.trim(),
            "invoiceAmount": invAmountElement.value.trim(),
            "invoiceDescription": descElement.value.trim()
        };

        const jsonPayload = JSON.stringify(jsonData);

        // Diagnostics
        console.log(jsonPayload);

        fetch(urlQuery, { mode: 'same-origin', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', body: jsonPayload }})

When I stringify it, and paste it in my Postman body, this is what I send, and the PHP side works fine. I send back { "result: true } and the Fetch crashes, because all these PHP error outputs are in the response payload.
{"projectNumber":"3352","userName":"xxxx","userType":"xxxx","invoiceClass":"EQ","invoiceOperation":"16","subContractor":"39","invoiceDate":"2021-03-27","invoiceNumber":"AZ-1000","invoiceAmount":"1.50","invoiceDescription":"Test Kit"}

I've never done this in plain valnilla JavaScript, and have been using Angular 8, in which I just stringify the object and Angular takes care of the rest. I searched around the internet and some examples show a pure string being fabricated, but the info dates back to 2011, nothing really 2020+ out there.

Browser response:
Note: I think I should keep the PHP side as is, because Postman works with it, and modify the JavaScript side. Versus trying to make the PHP side work with the current JavaScript side.

SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
But my browser F12, network, response says result "1"
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 72
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 73
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 74
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 75
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 76
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 77
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 78
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 79
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 80
Notice: Trying to access array offset on value of type null in C:\App\Dev\PCAD\api\invoices\viewInvoices.api.php on line 81
{
"result": "1"
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com


modified 27-Mar-21 16:01pm.

AnswerRe: Creating a JSON body payload in plain JavaScript [solved] Pin
jkirkerx27-Mar-21 11:52
professionaljkirkerx27-Mar-21 11:52 
GeneralRe: Creating a JSON body payload in plain JavaScript [solved] Pin
intoittendenz29-Mar-21 2:01
Memberintoittendenz29-Mar-21 2:01 
QuestionCannot figure out what code lines does mean ... Pin
Member 1501261727-Mar-21 1:01
MemberMember 1501261727-Mar-21 1:01 
AnswerRe: Cannot figure out what code lines does mean ... Pin
Member 1501261727-Mar-21 1:03
MemberMember 1501261727-Mar-21 1:03 
AnswerRe: Cannot figure out what code lines does mean ... Pin
Peter_in_278027-Mar-21 1:54
professionalPeter_in_278027-Mar-21 1:54 
GeneralRe: Cannot figure out what code lines does mean ... Pin
Member 1501261727-Mar-21 22:40
MemberMember 1501261727-Mar-21 22:40 
GeneralRe: Cannot figure out what code lines does mean ... Pin
Member 150126178-Apr-21 23:49
MemberMember 150126178-Apr-21 23:49 
AnswerRe: Cannot figure out what code lines does mean ... Pin
Richard Deeming28-Mar-21 23:29
mveRichard Deeming28-Mar-21 23:29 
GeneralRe: Cannot figure out what code lines does mean ... Pin
Member 150126178-Apr-21 23:49
MemberMember 150126178-Apr-21 23:49 
QuestionRedirect with (Page Visibility API) Pin
ab smine19-Mar-21 11:38
Memberab smine19-Mar-21 11:38 
QuestionNode-Schedule Module Can't Cron Job Scheduled Long Time Pin
Barış KAHRAMAN19-Mar-21 11:02
MemberBarış KAHRAMAN19-Mar-21 11:02 
QuestionCalculation in javascript Pin
Krasimir Dermendzhiev16-Mar-21 4:31
MemberKrasimir Dermendzhiev16-Mar-21 4:31 
AnswerRe: Calculation in javascript Pin
Richard Deeming16-Mar-21 5:39
mveRichard Deeming16-Mar-21 5:39 
GeneralRe: Calculation in javascript Pin
Krasimir Dermendzhiev16-Mar-21 22:36
MemberKrasimir Dermendzhiev16-Mar-21 22:36 
GeneralRe: Calculation in javascript Pin
Krasimir Dermendzhiev30-Mar-21 5:22
MemberKrasimir Dermendzhiev30-Mar-21 5:22 
QuestionJavascript CSP and CORS problem? How can I send Cookies with AJAX in CORS? Pin
Member 1510027614-Mar-21 22:50
MemberMember 1510027614-Mar-21 22:50 
AnswerRe: Javascript CSP and CORS problem? How can I send Cookies with AJAX in CORS? Pin
Richard Deeming14-Mar-21 23:39
mveRichard Deeming14-Mar-21 23:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.