2013-04-18 50 views
2

我有一個ajax調用,將數據傳遞給另一個php文件createTest2.php,如下所示。AJAX調用成功,但在服務器上未檢索到數據

但createTest2.php文件會拋出錯誤

"Notice: Undefined index: aaa in C:\xampp\htdocs\TestProj\Test\createTest2.php on line 2 

我不知道如何解決它。

caller.php

$(document).ready(function(){ 
    $("#button_submit").click(function() 
    { 

    $.ajax({ 
     type:"POST", 
     url:"createTest2.php", 
     data:{aaa : "UNIT_TEST"}, 
     success:function() 
     { 
     alert("success"); 
     } 
    }); 
}); 
}); 

createTest2.php

$test_name = $_POST['aaa']; 
+1

入住螢火蟲怎麼回事在該頁面的文章createTest2.php中,在該頁面上放置'if(isset($ _ POST)){echo $ test_name = $ _POST ['aaa']; }' – saveATcode

+0

你是否試圖在瀏覽器中打開createTest2頁面?..因爲即使你做了isset也不會顯示annything .. ur $ .ajax發送異步請求..這隻會在你的響應成功函數中看到.. download螢火蟲,看看你得到什麼迴應 – Dinesh

回答

0
if you are using 
    $test_name = $_POST['aaa']; 
you have to call ajax like this 
$(document).ready(function(){ 
    $("#button_submit").click(function() 
    { 

    $.ajax({ 
     type:"POST", 
     url:"createTest2.php", 
    data:"aaa=UNIT_TEST", 
     success:function() 
     { 
     alert("success"); 
     } 
    }); 
}); 
}); 

主要的事情是, 「數據:」 AAA = UNIT_TEST 「」

0

試試這個:

//sample.php 
<?php 
if(isset($_POST) && isset($_POST['aaa'])){ 
    echo json_encode("hello world! Your data was: " . $_POST['aaa']); 
} 
?> 

//your client side page 
<html> 
<head> 
    <title></title> 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script type="text/javascript"> 

     function send(){ 
      $.ajax({ 
       type:"POST", 
       url: 'sample.php', 
       data:{aaa:"UNIT_TEST"}, 
       dataType: 'json' 
      }).done(function(data){ 
       alert(data); 
      }); 
     } 

    </script> 
</head> 
<body> 
<a href="#" onclick="send();">Send</a> 
</body> 
</html> 
0

在,例如,index.html的試試這個:

<script> 
$(document).ready(function(){ 
    $("#button_submit").click(function() 
    { 

    $.ajax({ 
    global: false, 
    type:"post", 
    dataType: "html", 
    cache: false, 
    url: "createTest2.php", 
    data: {aaa:'test'}, 
    success:function(html) { alert(html); }, 
    error: function(e) {alert(e);} 
    }); 
}); 
}); 
</script> 

而在你createTest2.php只是把這個,看看是否收到Ajax調用:

<?php 
echo $_POST['aaa']; 

?> 
相關問題