2014-05-01 6 views
0

아래 코드에서 두 매개 변수를 Ajax 호출에 전달하려고합니다. 그러나 페이지를로드 할 때 아래에 표시된 JavaScript 오류가 발생합니다. 이 오류는 버튼 클릭으로 아약스를 트리거하려고하기 전에 발생합니다 (아래 표시된 코드는 작동 버튼 클릭 이벤트 핸들러 코드에 있음). 여러 변수를 전달하는 Jquery .post()가 작동하지 않습니다.

enter image description here

내가 코드의 .post() 블록을 주석

, 어떤 자바 스크립트 오류없이 잘 페이지가로드. 다음은 코드입니다. 이 게시물을 참조하는 방법을 수행 한 후 ajax .post()에 2 개의 매개 변수를 전달하는 것은 이번이 처음입니다. Sending Multiple variables with Jquery ajax

누구든지 내 코드에 무슨 문제가 있다고 말할 수 있습니까? 내 경고 문은 모두 올바르게 작동하고 올바른 데이터를 표시합니다. Cakephp를 사용하고 있으므로 약간의 cakephp 문법이 있습니다.

 //Get the merchant config 
     alert("you got here!"); 

     bookingdate = $(this).parent().parent().find("#bookingdate").serialize(); 
     merchant_id = $(this).parent().parent().find("#merchant_id").serialize(); 

     alert (bookingdate + " " + merchant_id); 

     $.post("<?php echo Router::url(array('controller'=>'MCP','action'=>'getMtRestaurant')); ?>", {bookingdate, merchant_id}, function() { 
     }) 
     .done(function(data) { 
      alert(data.response); 
      //parseddata = JSON.parse(data); 
      $('.ajax_booking_edit').append(data); 

     }) 
     .fail(function() { 
      alert("There was a problem retrieving the booking!"); 
     }) 
     .always(function() { 
      //alert("finished (always)"); 
     }); 

나는 serialize 된 후 bookingdate 및 merchant_id를 표시하는 알림 팝업 아래에 표시됩니다. enter image description here

+0

당신은 JQuery와, 즉'잘못된

+0

안녕하세요 .. 제가 Cakephp 코드를 언급 했으므로 .post() 호출에서 올바른 URL을 출력합니다. – aDvo

답변

0

함께의

serialize() 모두를보십시오.

var bookingdate = $(this).parent().parent().find("#bookingdate"), 
    merchant_id = $(this).parent().parent().find("#merchant_id"), 
    filterdata = bookingdate.add(merchant_id).serialize(); 
$.post("<?php echo Router::url(array('controller'=>'MCP','action'=>'getMtRestaurant')); ?>", 
filterdata, function() {}) 
+1

고마워요,이게 쉬운 것처럼 보입니다. – aDvo

+0

@aDvo 기쁜 마음으로 도움이되었습니다. –

1

하나의 값을 전달하면 트릭을 사용할 수 있습니다.

예 :이 도움이 PHP 파일을 수행에서

var msg = $arg1 + "--" + $arg2 + "--" + $arg3; 

    $.post("../Php/test.php", { msg: msg }, 
          function (result) { 
           alert(result); 
          }); 

,

<?php 
    $msg = $_POST['msg']; 
    $ar = explode("--", $msg); 
    //Now we have $ar[0] --> arg1, $ar[1] --> $arg2, $ar[2] --> $arg3 
?> 

희망.

0

jQuery를 통해 POST를 보내려면 사용중인 변수의 변수 이름을 선택해야합니다. ? 수정은 간단 단지에 행을 변경 :

$.post("<?php echo Router::url(array('controller'=>'MCP','action'=>'getMtRestaurant')); ?>", {bookingDate: bookingdate, merchantId: merchant_id}, function() {