JSON.parse:JSON 数据第 1 行第 5 列处的 JSON 数据后出现意外的非空白字符

问题描述 投票:0回答:1

我已将

phpmailer 6.8.1
(从
phpmailer 5.2
升级)安装到我的服务器中(使用
php v7.0.33
)。当我收到测试电子邮件时,phpmailer 正在工作,但是一旦处理/发送了电子邮件数据,
ajax
调用就会返回到调用页面,在该页面中我向用户显示电子邮件已发送的消息,这就是我正在查找的部分纠正。网站的
contact.php
页面通过
process_form.php
调用将表单数据发送到
ajax

    form.submit.addEventListener('click', function(){
        let first_name = document.getElementById('first_name').value;
        let telephone = document.getElementById('telephone').value;
        let email = document.getElementById('email').value;
        let textarea1 = document.getElementById('textarea1').value;
        let submit = document.getElementById('btn-submit').value;
        let messages = document.getElementById('form-messages').value;

        const request = new XMLHttpRequest();
        //debugger;
        /* WHEN FUNCTION COMES BACK FROM AJAX CALL WE RUN THIS FUNCTION */
        request.onload = function() {
            console.log('line 318 ', request.responseText);//
            let responseObject = null;
            /* CONVERTING JSON STRING INTO A STANDARD JS OBJECT */
            try{
                responseObject = JSON.parse(request.responseText);
                //console.log(responseObject);

            } catch (e) {

                console.error('Could not parse JSON! (line ~327 in js code)', e.message);

            }
            /* IF THERE WAS NO ERROR - PASS INTO FUNTION */
            if (responseObject) {
                handleResponse(responseObject);
            }
        }; //END: request.onload = function() {
        const requestData = 'first_name='+first_name+'&telephone='+telephone+'&email='+email+'&textarea1='+textarea1;
        console.log('requestData = ' + requestData);

        /* MAKING A POST REQUEST HERE */
        request.open('post', '../scripts/process_form.php');
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        /* SENDING REQUEST BELOW */
        request.send(requestData);
    }); //END:

这是页面控制台中的错误消息:

e: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 5 of JSON data

来自

json_encode
process_form.php
在处理完表单数据后发送。此时,在代码中,它是在发送电子邮件之后(验证所有表单字段)或没有发送电子邮件,因为表单字段中记录了错误,并且它指定了哪个表单字段具有不正确的数据(因此是数组) :

    echo json_encode(
        array(
            'ok' => $ok,
            'messages' => $messages
        )
    );

$ok
是布尔值,指示用户输入是否正确
$messages
表示字符串“成功发送电子邮件”或指定通过表单传递的错误字段。

我没有从

process_form.php
收到任何信息,并且在
contact.php
上调试时:

console.log('responseObject = ', responseObject);

responseObject
null

更新:我没有正确读取控制台日志文件,对于混淆表示歉意,但现在我可以在代码中查看日志消息,这就是我所看到的:

对于:

       request.onload = function() {
            console.log('line 318 (responseText = ) ', request.responseText);//

我知道我发送正确:

        const requestData = 'first_name='+first_name+'&telephone='+telephone+'&email='+email+'&textarea1='+textarea1;
        console.log('line 344 (requestData = ) ' + requestData);

        /* MAKING A POST REQUEST HERE */
        request.open('post', '../scripts/process_form.php');
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        /* SENDING REQUEST BELOW */
        request.send(requestData);

来自控制台日志:

line 344 (requestData = ) first_name=Brian D Rauch&telephone=6092843554&[email protected]&textarea1=Email Test 9/27/2023 (28)

而且

responseText
可能很混乱 - 信息太多,不知道它来自哪里:

line 318 (responseText = )  2023-09-27 19:08:23 CLIENT -&gt; SERVER: EHLO allcountyguitar.com<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: STARTTLS<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: EHLO allcountyguitar.com<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: AUTH LOGIN<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: [credentials hidden]<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: [credentials hidden]<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: MAIL FROM:&lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: RCPT TO:&lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: RCPT TO:&lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: RCPT TO:&lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: DATA<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Date: Wed, 27 Sep 2023 19:08:23 +0000<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: To: [email protected]<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: From: &quot;AllCountyGuitar (via KiteandWindsurfer)&quot; &lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Cc: [email protected]<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Reply-To: Information &lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Subject: Inquiry from AllCountyGuitar Website!<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Message-ID: &lt;[email protected]&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: X-Mailer: PHPMailer 6.8.1 (https://github.com/PHPMailer/PHPMailer)<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: MIME-Version: 1.0<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: Content-Type: text/html; charset=iso-8859-1<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: <br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: <br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;div style=&#039;color:#33691e;background-color:#f1f8e9; font-family: Tahoma; padding:20px; width:60%; border:solid 1px #dcedc8;&#039;&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;h1&gt;Email Inquiry From AllCountyGuitar.com:&lt;/h1&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;p&gt;Date: Wed, Sep 27, 2023&lt;/p&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;p&gt;Name: Brian D Rauch&lt;/p&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;p&gt;Telephone: 6092843554&lt;/p&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;p&gt;Email Address: [email protected]&lt;/p&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;br&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;div&gt;Comment:&lt;br&gt;&lt;br&gt;Email Test 9/27/2023 (27)&lt;/div&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;h3&gt;END OF MESSAGE&lt;/h3&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:             &lt;/div&gt;<br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER:         <br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: <br>
2023-09-27 19:08:23 CLIENT -&gt; SERVER: .<br>
2023-09-27 19:08:24 CLIENT -&gt; SERVER: QUIT<br>
{"ok":true,"messages":["Thank you, your message has been sent!"]}

最后,

responseObject
永远不会超越
null
responseObject = JSON.parse(request.responseText);

更新结束

请回复并提供任何需要的进一步信息。

最好的, 布莱恩

php json ajax phpmailer
1个回答
0
投票

//$mail->SMTPDebug = 1;
解决了这个问题。然而,如果没有 @David、@aynber 和 @tkauslVERY

有用的评论,这是不可能实现的。

phpmailer 的升级很有压力,因为我不是技术恐惧者,因此非常犹豫。早期我设置了调试参数,直到追根溯源才注意到它。再次非常感谢所有为此提供帮助的人,以及将 StackOverflow 作为探索和寻求帮助的论坛。

对大家来说最好的是, 布莱恩

© www.soinside.com 2019 - 2024. All rights reserved.