繁体   English   中英

Ajax调用网址数据中的正斜杠

[英]Forward slash in ajax call url data

我正在尝试将服务器数据解析为REST API,以存储在数据库中。 但是数据似乎没有被解析。 如果我按如下所示对网址进行硬编码,则数据将成功发布,

$.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

没有内容,只有目标网址。

您的第二个示例发送这种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

您的目标网址不同,然后数据在请求正文中。

如果将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);
   }
 });  

当您使用$.ajax data时,结果将作为表单数据添加(因为您使用type: "post" )。

从您的问题来看,您似乎想要:

$.ajax({
  url: 'index.php/rest/resource/questions/uId/'+qUserId+'/qTitle/'+qTitle+'/qBody/'+qBody+'/qTag/1',
  success: function(data) {
    alert(data);
  },
  type: "post"      
});

我要指出,然而,使用data是一个更好的选择POST -calls如果你发送大量的数据,然后只通过看POST数据的服务器上(如$_POST['qBody']在PHP)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM