Ajax无法将数据完全发布到php文件

Ajax无法将数据完全发布到php文件

本文介绍了Ajax无法将数据完全发布到php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Ajax没有将整个字符串数据发送到php脚本而遇到了一个非常奇怪的错误.

Getting a really strange error with Ajax not sending the entire string data across to the php script.

字符串是

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;
font-family:&quot;Verdana&quot;,sans-serif;mso-ansi-language:DE">Gold GoldGold Gold Gold Gold<o:p></o:p></span></u></b></p>

<p class="MsoNormal" style="text-align:justify"><span lang="EN-GB" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;font-family:&quot;Verdana&quot;,sans-serif">&nbsp;</span></p>"

但是到达php脚本的只是(通过数据库提交和php $ result测试可以看到.

but what arrives at the php script is only (Seen through DB submission and the php $result test.

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;
font-family:"

我试图通过多种方法检查为什么会发生这种情况,但根本无法弄清楚.

I have tried to check through multiple methods of why this is happening but just cant figure it out at all.

这是我的Javascript代码和php代码.

Here is my Javascript code and php code.

JS:

function Send_upload_news()
    {
        get_title = document.getElementById('Germ_title');
        get_date = document.getElementById('Germ_date');

        input_title = get_title.value;
        input_date = get_date.value;
        input_content = $("div.nicEdit-main").html();

        //console.log(input_content);

        var Data = '&input_title='+input_title+'&input_date='+input_date+'&input_content='+input_content;
        //console.log(Data);
        $.ajax({
            url : "uploadNews.php",
            type: "POST",
            dataType: 'text',
            data : Data,
            success: function(result){alert(result);},
            /* success: function() {
                 alert('Post has been uploaded to Database');
            }, */
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
           }
        });
        nicEditors.findEditor( "Germ_content" ).setContent( '' );
        get_title.value = "";
        get_date.value = "";
        $('html, body').animate({ scrollTop: 0 }, 'slow');

    };

pHp:(uploadNews.php)

pHp:(uploadNews.php)

<?php
//Database info
$db_host = "localhost";
$db_username = "";
$db_pass = "";
$db_name = "";
//connect db
$connMS = new mysqli ( $db_host, $db_username, $db_pass, $db_name );
//grab data

$this_title = $_POST['input_title'];
$this_date = $_POST['input_date'];
$this_content = $_POST['input_content'];

$result = file_put_contents ( "test.txt", $this_content);
//create query and execute
$sqlupdate_news = "INSERT into news_content(germ_title, germ_date, germ_content) values ('$this_title','$this_date','$this_content')";
mysqli_query ($connMS,$sqlupdate_news);
//close
mysqli_close($connMS);
?>

我使用WYSWYG Nicedit作为我的文本区域

Im using WYSWYG Nicedit as my text area

如果有人可以帮助我解决这个问题,我将非常感激.

If there is somebody that can help me figure this out I would be very grateful.

推荐答案

您的代码存在的问题是您未对值进行编码.这样会搞乱值.另外,您还有一个领先的& ,这毫无意义.您可以使用encodeURIComponent手动编码所有值,也可以让jQuery为您处理.

The problems with your code is you are not encoding the values. So that will mess up the values. Plus you have a leading & which makes no sense. You can manually encode all the values with encodeURIComponent or you can let jQuery handle that for you.

当您使用对象时,jQuery会为您进行编码.

jQuery does the encoding for you when you use an object.

function Send_upload_news() {

    var get_title = document.getElementById('Germ_title');
    var get_date = document.getElementById('Germ_date');

    var Data = {
        input_title : get_title.value,
        input_date : get_date.value,
        input_content : $("div.nicEdit-main").html()
    };

    $.ajax({
        url : "uploadNews.php",
        type: "POST",
        dataType: 'text',
        data : Data,
        success: function(result){alert(result);},
       error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
       }
    });

    nicEditors.findEditor( "Germ_content" ).setContent( '' );
    get_title.value = "";
    get_date.value = "";
    $('html, body').animate({ scrollTop: 0 }, 'slow');
}

您确实应该使用 var ,这样您的应用程序不会充满全局变量.

And you really should use var so your application is not full of global variables.

这篇关于Ajax无法将数据完全发布到php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:37