dousi8559 2013-08-04 19:39
浏览 26
已采纳

将序列化数据附加到url

I am using a flight search api. My question is in my form, if I send the form fields to an ajax file as

var data = $('form').serialize();

Is there a way in my ajax file to append serialize() string sent to my api url. Right now my ajax file looks like this and although it works, it is very messy and code needs refactored:

require('lib/Unirest.php'); 

if(isset($_POST['departureAirport'])){
    $customerSessionId = $_POST['customerSessionId'];
    $departureAirport = $_POST['departureAirport'];
    $destinationAirport = $_POST['destinationAirport'];
    $departureDate = $_POST['departureDate'];
    $returnDate = $_POST['returnDate'];
    $adults = $_POST['adults'];





$getdata = Unirest::get("http://test.api.theapidomain.com/airticket/v1/list.aspx?cid=N7Y5C1&customerSessionId=".$customerSessionId."&departureAirport=".$departureAirport."&destinationAirport=".$destinationAirport."&departureDate=".$departureDate."&returnDate=".$returnDate."&adults=".$adults."", array( "Accept" => "application/json" )
);

I was really hoping for something like this:

$dataSent = //the serialized data sent
$getdata = Unirest::get("http://test.api.theapidomain.com/airticket/v1/list.aspx? + $dataSent, array( "Accept" => "application/json" )
    );

here is my jquery

<script type="text/javascript">
   $('#myform').submit(function(e){
         e.preventDefault();   
        var data = $('form').serialize(); 
        $.ajax({  
            type: "POST",  
            url: "ajax.php",  
            data: data,
            success: function(response){
            $("#result").html(response);
            }
    });
});
</script>
  • 写回答

2条回答 默认 最新

  • dougong1031 2013-08-04 19:45
    关注

    Send your form data as a string

    Javascript

    $('#myform').submit(function(e){
        e.preventDefault();   
        $.ajax({  
            type: "POST",  
            url: "ajax.php",  
            data: {data: $('form').serialize()},
            success: function(response){
                $("#result").html(response);
            }
    });
    

    Or simplified using $.post()

    $('#myform').submit(function(e){
        e.preventDefault();   
        $.post("ajax.php", {data: $('form').serialize()}, function(response){
            $("#result").html(response);
        });
    });
    

    PHP

    $dataSent = isset($_POST['data']) ? $_POST['data'] : NULL
    $getdata = Unirest::get("http://test.api.theapidomain.com/airticket/v1/list.aspx?" . $dataSent, array( "Accept" => "application/json" ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题