ajax调用url数据中的正斜杠


Forward slash in ajax call url data

我正在尝试将服务器数据解析为REST API作为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';
} 
}    
?>

从Rest Controller使用的学生模型

<?php
class Student extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function askQ($args)
    {

        $timeVal = date( "Y-m-d H:i:s", mktime(0, 0, 0));            
        $qVotes = 0;
        $qStatus = 1;       

        if (!isset($args['uId']) || !isset($args['qTitle']) || !isset($args['qBody']) || !isset($args['qTag'])) {
    return false;
        }
        $this->db->insert('questions',array('userId' => $args['uId'],'questionTitle' => $args['qTitle'],'questionBody' => $args['qBody'],'tagQuestionId' => $args['qTag'],'postDate' => $timeVal,'status' => $qStatus,'votes' => $qVotes));
        return true;
    }
}   
?>

您也应该了解一下您的REST服务器。您的示例产生明显不同的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

没有内容,只有目标url。

您的第二个示例发送这种HTTP请求:

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

您的目标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    


更新

所以,若要求使您的第一个请求是动态的,您必须手动构建目标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中。您应该将其作为正确的POST请求发送。因此,我将设计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/ 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

您可以通过对参数进行编码来完成以下操作

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);
   }
 });  

使用$.ajaxdata时,结果将作为表单数据添加(因为使用type: "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中的$_POST['qBody']),则使用dataPOST调用的更好选择