使用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.

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 ); });

jQuery expects an object as data, remove the double-quotes:

data: { abc : val }