【问题标题】:Trailing html with ajax response form php带有ajax响应表单php的尾随html
【发布时间】:2021-11-08 18:07:45
【问题描述】:

我正在尝试将信息从 html 表单发送到 php 文件。表单工作得很好,php 可以正确处理信息,但是我在回调的响应中遇到了问题。

这里是javascript代码:

function jvsSubmit(fd){
var submitUrl = 'repeater_field_capture.php';

$.ajax({
    type:'post',
    url: submitUrl,
    data: fd,
    contentType: false,
    processData: false,
    success: function(response){ submit_settings_form_callback(response); },
});
}
function submit_settings_form_callback(data){



console.log(data);
}

这里是php代码:

if(isset($_POST['formInfo']))
{
update_option('text',$_POST['text']);

$return = ['success' => 1, 'message' => 'Message Sent'];

echo json_encode($return);

}

除了来自回调的响应外,一切正常。这是我在控制台记录回调中的数据时看到的内容。

{"success":1,"message":"Message Sent"}<!doctype html>
<html lang="en-US" >
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Page not found &#8211; DB Website Projects</title>
<meta name='robots' content='max-image-preview:large' />
<link rel='dns-prefetch' href='//s.w.org' />
<link rel="alternate" type="application/rss+xml" title="DB Website Projects &raquo; 
Feed" href="https://projects.db-websites.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="DB Website Projects &raquo; 
Comments Feed" href="https://projects.db-websites.com/comments/feed/" />
    <script>
        window._wpemojiSettings =........

您可以看到 json 对象在那里,但有一些导致问题的尾随 html。

【问题讨论】:

    标签: javascript php jquery ajax


    【解决方案1】:

    您可以通过在echo json_encode($return); 行之后添加die()exit() 来停止PHP 脚本的其余部分的执行。

    【讨论】:

    • 非常感谢!那行得通。
    猜你喜欢
    • 2011-05-13
    • 2019-09-03
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 2018-03-20
    • 2017-09-08
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多