dongshuo5101 2014-02-25 11:19
浏览 73

使用jquery ajax将数据从js文件传递到php

ajaxcheck.js

var val = "hai";

 $.ajax(
 {
 type: 'POST',
 url: 'ajaxphp.php',
 data: { "abc" : val },

success :function(data)
 {
    alert('success'); 
 }

 }

 )
.done(function(data) { 
alert("success :"+data.slice(0, 100));

 }

 )
.fail(function() { 

alert("error"); 

}
);

ajax.html

 <!DOCTYPE html >

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
 </script>

  <script type="text/javascript" src="ajaxcheck.js"></script>

  <title>ajax request testing</title>
  </head>

  <body>
  </body>
  </html> 

ajaxphp.php

<?php

$v_var = $_POST["abc"];

print_r($_POST);

if(isset($_POST["abc"]))
{
echo $v_var;
}
else
{
echo "Data not received";
}
?>

When I run the ajax.html file ,I get success alert. But when I run ajaxphp.php file it shows notice like:

undefined index abc 

Why is that the data is not received in $v_var ? where i am mistaking ?please help.

  • 写回答

2条回答 默认 最新

  • ds211107 2014-02-25 11:25
    关注

    Actually the ajaxphp.php file is used to receive the data from the post method and i will give response .

    In First case by using ajax post method it will call to ajaxphp.php file.

    In Second case your using direct file call without any post data(that's way it shows error)

    Try this

    var val = "hai" $.post( "ajaxphp.php", {'abc':val}function( data ) { alert( "Data Loaded: " + data ); });

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥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