Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php在使用ajax post提交后丢失了一些字符串路径_Php_Jquery_Ajax - Fatal编程技术网

php在使用ajax post提交后丢失了一些字符串路径

php在使用ajax post提交后丢失了一些字符串路径,php,jquery,ajax,Php,Jquery,Ajax,我正在尝试使用ajax post和php提交评论,它工作得很好,正如我预期的那样,但是当我尝试提交一些包含&的sting时,它丢失了sting的路径,只显示从开始到和所在的位置。 我真的不知道如何解决这个问题,我不知道是什么问题,请我需要帮助 参见示例 我的ajax <script> $('#submitcomment').on('click', function(){ try{ var message = $('.co

我正在尝试使用ajax post和php提交评论,它工作得很好,正如我预期的那样,但是当我尝试提交一些包含
&
的sting时,它丢失了sting的路径,只显示从开始到和所在的位置。 我真的不知道如何解决这个问题,我不知道是什么问题,请我需要帮助

参见示例

我的ajax

<script>
    $('#submitcomment').on('click', function(){
        try{
                    var message = $('.commentTextinput').val();
                    var key= $(this).attr('data-keyname');

                    $.ajax({
                        url: UrlExistsA('snippet/snippetcomment'),
                        data: 'message=' + message + '&key=' + key,
                        type: 'POST',
                        beforeSend: function(){
                            $('#submitcomment').html('Wait....');
                        },
                        success: function(data){
                            $('tr.replyList:last-child').after(data);
                            $('.commentTextinput').val('');
                            $('#submitcomment').html('Comment');
                        },
                        error: function(data){
                            alert('Processing Error' + '<br/>' + data);
                        }                   
                    });                 
        }catch(err){alert(err.message);}
        finally{}
    });
</script>

$(“#提交建议”)。在('click',function()上{
试一试{
var message=$('.commentTextinput').val();
var key=$(this.attr('data-keyname');
$.ajax({
url:UrlExistsA('snippet/snippetcomment'),
数据:'message='+message+'&key='+key,
键入:“POST”,
beforeSend:function(){
$('#submitcomment').html('Wait…');
},
成功:功能(数据){
$('tr.replyList:last child')。在(数据)之后;
$('.commentTextinput').val('');
$('#submitcomment').html('Comment');
},
错误:函数(数据){
警报('处理错误'+'
'+数据); } }); }catch(err){alert(err.message);} 最后{} });
这里是测试php

<?php
if(isset($_POST['message'])){
$postReply = htmlentities($_POST['message'], ENT_QUOTES, "UTF-8");
echo $postReply;

}

您需要调用
encodeURIComponent
对参数字符串中的特殊字符进行编码
&
是参数之间的分隔符,因此需要对其进行编码

data: 'message=' + encodeURIComponent(message) + '&key=' + encodeURIComponent(key),
但更简单的方法是使用对象而不是字符串,然后jQuery将自动对其进行编码

data: { message: message, key: key },

如果字符串只有一个,我还可以将其添加为对象吗<代码>数据:{id:id}
?是的,这没什么特别的。