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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号