1

我想将 2 个参数从客户端( jquery )端发送到服务器端( php-codeigniter )。参数是 id(一个整数)和 text(一个字符串)。我试过了:

window.location.href="AjaxUpdate/sendMessage/"+id+"/"+text;

过去只使用 id 对我有用,但是因为文本字符串可以有空格,所以我可以得到:

http://localhost/b1/mycontroller/myfunction/3/gdfgdgd%20 %20 gd 

这使:

An Error Was Encountered
The URI you submitted has disallowed characters.

使用 jquery 准备字符串并将其传输到服务器端的最佳方法是什么?我认为 ajax 将以某种方式使用

4

1 回答 1

7

You can use the jQuery .ajax() function to POST data (JSON) to your PHP script, like this:

var postData = {
    "param1" : some_value1,
    "param2" : some_value2
};

console.log(postData);

$.ajax({
    type: "POST",
    url: "test.php",
    data: postData, 
    success: function(){
        alert(param1 + ' ' + param2);
        window.open("test.php"); 
    }
});

UPDATE:

PHP sample that I Googled and found; it doesn't seem to do much, but unfortunately I am not versed in PHP at all:

//request parameters
$param_1 = $_POST['param1'];
$param_2 = $_POST['param2'];

echo 'postData: ' . var_dump($_POST);

if ($_POST)){
    echo $param_1;
    echo $param_2;
} else {
    echo 'problem';
}
于 2013-08-13T01:54:31.213 回答