Javascript ajax调用url数据中的正斜杠
我试图将服务器数据解析为RESTAPI,作为post存储在数据库中。但是数据似乎没有被解析。如果我硬编码url如下,数据将成功发布Javascript ajax调用url数据中的正斜杠,javascript,php,ajax,rest,Javascript,Php,Ajax,Rest,我试图将服务器数据解析为RESTAPI,作为post存储在数据库中。但是数据似乎没有被解析。如果我硬编码url如下,数据将成功发布 $.ajax({ url: 'index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1', uccess: function(data) { alert(data); }, type: "post" }); 但是,如果我尝试按如下方式动态
$.ajax({
url: 'index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1',
uccess: function(data) {
alert(data);
},
type: "post"
});
但是,如果我尝试按如下方式动态插入数据,则它不起作用:
$.ajax({
url: 'index.php/rest/resource/questions/',
data: { 'uId\/':qUserId, '\/qTitle\/':qTitle, '\/qBody\/':qBody, '\/qTag\/':'1' },
success: function(data) {
alert(data);
},
type: "post"
});
这个问题可能有一个简单的解决方案,但我无法从网上找到的资源中得到任何积极的结果
编辑
当我在浏览器上手动粘贴带有硬编码数据的查询时,如下所示
index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1
T提交要存储在数据库中的数据。我的要求是通过ajax调用中的变量将变量动态插入url:)
编辑2
休息控制器代码
<?php
class Rest extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('student');
$this->load->helper('url');
}
// we'll explain this in a couple of slides time
public function _remap()
{
// first work out which request method is being used
$request_method = $this->input->server('REQUEST_METHOD');
switch (strtolower($request_method)) {
case 'post' : $this->doPost(); break;
default:
show_error('Unsupported method',404); // CI function for 404 errors
break;
}
}
public function doPost(){
$args = $this->uri->uri_to_assoc(2);
switch ($args['resource']) {
case 'questions' :
$res = $this->student->askQ($args);
if ($res === false) {
echo json_encode(array('error' => 'unable to post','status' => 1));
}
else {
echo json_encode(array('status' => 0));
}
break;
default:
show_error('Unsupported resource',404);
}
echo 'posted';
}
}
?>
您可以通过编码参数来执行以下操作
var data = 'uId/='+qUserId+'&/qTitle/='+qTitle+'&/qBody/='+qBody+'&/qTag/=1';
data = encodeURI(data);
$.ajax({
type: "post",
url: 'index.php/rest/resource/questions/',
data: data,
success: function(data) {
alert(data);
}
});
当您使用$.ajax
的数据时,结果将作为表单数据添加(因为您使用键入:“post”
)
从你的问题来看,你似乎想要:
$.ajax({
url: 'index.php/rest/resource/questions/uId/'+qUserId+'/qTitle/'+qTitle+'/qBody/'+qBody+'/qTag/1',
success: function(data) {
alert(data);
},
type: "post"
});
但是,我应该指出,如果您发送大量数据,然后只需查看服务器上的POST
数据(例如PHP中的$\u POST['qBody']
),那么使用data
调用POST是一个更好的选择。您的示例生成明显不同的HTTP请求,我认为您的服务器代码不能同时处理这两种请求:
您的第一个示例发送此类HTTP请求:
POST /index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1 HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 0
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 68
uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Length: 47
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
uId=123&qTitle=some+title&qBody=body+txt&qTag=1
没有内容,只有目标url
您的第二个示例发送此类HTTP请求:
POST /index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1 HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 0
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 68
uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Length: 47
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
uId=123&qTitle=some+title&qBody=body+txt&qTag=1
您的目标url不同,因此数据位于请求正文中
如果您将ajax方法更改为GET,那么jQuery会将参数添加到url,但它仍然不等同于您的硬编码请求:
GET /index.php/rest/resource/questions/?uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1 HTTP/1.1
Host: some.server.invalid
更新
<?php
class Rest extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('student');
$this->load->helper('url');
}
// we'll explain this in a couple of slides time
public function _remap()
{
// first work out which request method is being used
$request_method = $this->input->server('REQUEST_METHOD');
switch (strtolower($request_method)) {
case 'post' : $this->doPost(); break;
default:
show_error('Unsupported method',404); // CI function for 404 errors
break;
}
}
public function doPost(){
$args = $this->uri->uri_to_assoc(2);
switch ($args['resource']) {
case 'questions' :
$res = $this->student->askQ($args);
if ($res === false) {
echo json_encode(array('error' => 'unable to post','status' => 1));
}
else {
echo json_encode(array('status' => 0));
}
break;
default:
show_error('Unsupported resource',404);
}
echo 'posted';
}
}
?>
所以,若要求使您的第一个请求成为动态的,您必须手动构建目标url:
$.ajax({
url: 'index.php/rest/resource/questions/uId/'+qUserId
+'/qTitle/'+encodeURIComponent(qTitle)
+'/qBody/'+encodeURIComponent(qBody)+'/qTag/1',
success: function(data) {
alert(data);
},
type: "post"
});
但是,如果你应该改变你的服务器代码,就值得考虑一下。如果你正在发送博客文章或类似的东西,你不应该把它放在url中。你应该按要求邮寄。因此,我将设计REST API,以便它接受此请求:
$.ajax({
url: 'index.php/rest/resource/questions/',
data: { 'uId':qUserId, 'qTitle':qTitle, 'qBody':qBody, 'qTag':'1' },
success: function(data) {
alert(data);
},
type: "post"
});
该示例发送此类HTTP请求:
POST /index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1 HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 0
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 68
uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Length: 47
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
uId=123&qTitle=some+title&qBody=body+txt&qTag=1
你在“成功”功能中缺少一个“s”。我不知道这是怎么回事=D@m59:D我看到了,谢谢你指出:)但数据仍然没有发布谢谢你的回复,但它说它无法发布:(不知道原因是什么谢谢你的回复,我一开始尝试了这个解决方案。但没有帮助:(再次感谢reply@Hasitha我怀疑这与q
变量的内容有关。您是否可以设置一个包含值的变量作为不起作用的代码?我希望可以,但问题是这是一个使用php的MVC项目。使用您的解决方案在web控制台firefox中也会出现错误的请求:(@Hasitha:然后尝试只记录url(在执行$.ajax
之前):console.log('index.php/rest/resource/questions/uId/'+qUserId+'/qTitle/'+qTitle+'/qBody/'+qBody+'/qTag/1')
并发布结果。错误的请求通常是由于无效的url。您好,我为上述ajax调用添加了控制器和模型代码:)@Hasitha我不熟悉CI,但是如果你想使用POST方法,你对数据的访问似乎是$this->input->POST
而不是$this->url
。也就是说,如果使用你的原始代码,$this->input->POST('uId/)
应该给你uId。如果使用我的上一个例子,$this->input->POST('uId'))
给你uId。谢谢。你的答案包括URL连接URL:'index.php/rest/resource/questions/uId/'+qserid+'/qTitle/'+encodeURIComponent(qTitle)+'/qBody/'+encodeURIComponent(qTitle)+'/qTag/1'
成功了:)再次感谢:)