0

我有这个jQuery AJAX函数,它从 a 中检索数据CKEDITOR text_area并将其发送AJAX.php文件:

function saveEmailData(){
    var text_body = encodeURIComponent(CKEDITOR.instances.text_body.getData());

    $.ajax({
        url: 'al_ajax.php',
        cache: false,
        data: {area: text_body, use: 'savemaildata'},
        type: 'POST',
        success: function(result){
                    alert(result);
                 },
        error: function(result){
                    alert('Error');
                },
    });
}

PHP:

$area = $link->real_escape_string($_POST['area']);

$insert_query = $link->query("INSERT INTO ALLARME_EMAIL_TEXT (area)
                                VALUES ('$area')
                              ON DUPLICATE KEY UPDATE
                                area = '$area'
                              ")
                              or die('Error.');

if($insert_query){          
    echo 'OK!';
}

问题是使用jQuery AJAX,我将在数据库%20上使用正常javascript function,这不会发生:我总是使用。为什么?

function saveEmailData(){
    var text_body = encodeURIComponent(CKEDITOR.instances.text_body.getData());

    if(window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();     
    } else {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        alert(xmlhttp.responseText);
      }
    }

xmlhttp.open("POST","al_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("area="+text_body);
}
4

1 回答 1

2

这不是 jQuery 的错。您对文本正文进行了两次转义:

var text_body = encodeURIComponent(CKEDITOR.instances.text_body.getData());

注意 jQuery 已经encodeURIComponent对你作为data. 解决方案就是摆脱它。

function saveEmailData(){
    var text_body = CKEDITOR.instances.text_body.getData();

    $.ajax({
        url: 'al_ajax.php',
        cache: false,
        data: {area: text_body, use: 'savemaildata'},
        type: 'POST',
        success: function(result){
                    alert(result);
                },
        error: function(result){
                    alert('Error');
                },
    });
}
于 2013-08-22T14:42:03.370 回答