![](/img/trans.png)
[英]How to pass parameter from javascript to controller in codeigniter?
[英]How to pass safely text with URLs as a parameter to a CodeIgniter controller?
我试图通过jQuery通过AJAX从JavaScript发送,该字符串可能包含一个或多个URL。
该文本将由CodeIgniter控制器接收。
我有一些错误。 有时是404错误,有时是406错误,具体取决于我发送数据的方式。
现在,我将其发送为:
var dsPost = encodeURIComponent(base64_encode(postContent));
$.ajax({
url: "/posts/createTextPost/" + dsPost,
type: "POST",
data: "userIdWall" + "=" + userIdWall,
success: function(data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
base64_encode fn是phpjs实现。
在CI控制器中,我这样做:
public function createTextPost($dsPost) {
$dsPost = base64_decode(urldecode($dsPost));
}
关键是,数据可以保存到数据库,但我不明白为什么会出现404错误。
有任何想法吗?
谢谢。
base64_encode()
函数有时添加/
在编码的字符串,因此Codeigniter
动作方法表现得像second
paramameter
如果编码字符串像:qwqw221 @ 1223 / dds *(ewfd)
比ci问题
“ /”字符后的字符串部分,其行为类似于第二个参数
不允许使用“ *”字符
因此,您应该使用GET
查询字符串而不是CI
参数
喜欢
url: "/posts/createTextPost/?crypt=" + dsPost
并在CI
控制器操作中获取查询字符串
public function createTextPost() {
$dsPost = base64_decode(urldecode($this->input->get("crypt")));
}
我建议将dsPost添加到AJAX调用中的数据中,而不是将其作为参数添加到url中:
$.ajax({
url: "/posts/createTextPost/",
type: "POST",
data: { "dsPost": dsPost, "userIdWall", userIdWall },
success: function(data, textStatus, jqXHR) {
// Yippie!
},
error: function (jqXHR, textStatus, errorThrown) {
// Bhuhuhu....
}
});
...然后更新您的CI控制器以使用发布的对象而不是输入参数:
public function createTextPost() {
$dsPost = base64_decode( urldecode( $this->input->post('dsPost') ) );
userIdWall = $this->input->post('userIdWall');
}
您必须通过ajax数据发送内容,如下所示。
$.ajax({
//double check your url setting in this way in case you have diffrent setting for index.php then remove the index.php
url: "<?=base_url()?>.index.php/posts/createTextPost/",
type: "POST",
data: {
"dsPost": dsPost,
"userIdWall", userIdWall
},
success: function(data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
然后在您的控制器中
public function createTextPost() {
$dsPost = base64_decode( urldecode( $this->input->post('dsPost') ) );
$userIdWall=$this->input->post('userIdWall');
希望对您有所帮助。
因此,感谢您的想法,但可以在这里找到答案:
我引用那里给出的答案:
如果任何用户输入项以http://或https://开头,则您的网站将生成错误。
当我尝试以http://开头的链接时,出现了406不可接受:
http://onkore.us/?blah=http://www.google.com
当我尝试这个很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.