使用ajax从html编码和解码到php

时间:2015-07-23 07:38:13

标签: php jquery html ajax

backup.html:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<script>
var key=45;
var value=65;
var rated = {"key" : key , "value" : value};
var rated_encoded = JSON.stringify(rated);

$.ajax({
  type: "POST",
  url: "text.php",
  dataType:"json",
  contentType: "application/json; charset=UTF-8", 
  processData: false,
  data: {
    "rated" : rated_encoded
  },
  //success: function(data) {
    //document.write(data);
  //}
});
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
    $("#div1").load("text.php");
});
});
</script>
<div id="div1"><h6>Result</h6></div>

<button>Get External Content</button>

</body>
</html>

text.php:

<?php 
if(isset($_POST["rated"])){
$rated_json = $_POST["rated"];
$JSONArray  = json_decode($rated_json, true); 
if($JSONArray !== null){ 
    $key = $JSONArray["key"];
    $value = $JSONArray["value"];
}
echo $key;

} 
?>

我希望能够做的就是在text.php中打印$ key和$ value,以便它可以在backup.html中显示。 echo $ key无效。取而代之的是,回声&#34;你好&#34;也没有用。如果我输入echo&#34;那么&#34;我只能回应一些东西。在isset函数之外。如何打印$ key和$ value?谢谢!

0 个答案:

没有答案