我想在php中使用javascript数组。我用JSON、AJAX。。。我想把这个数组放在同一个文件中。但是我找不到。有人能帮忙吗?例如php:-
<?php
echo '<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
</script>
<script>
var arr = ["a", "b", "c"];
var str = JSON.stringify(arr);
$.ajax({
type:"POST",
url: "ex.php",
data:"jsonstr=" + str,
dataType:"json",
success: function(){
alert("Test results submitted!");
}
});';
echo '</script></head><body></body></html>';
?>
<?php
echo json_encode($_POST['jsonstr']);
?>
<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
</script>
<script>
var arr = ["a", "b", "c"];
var str = JSON.stringify(arr);
$.ajax({
type:"POST",
url: "",
data:"jsonstr=" + str,
dataType:"json",
done: function(response){
alert("Test results submitted!");
}
});
</script></head></html>
<?php
echo json_encode($_POST['jsonstr']);
?>
试试这个。
不要总是回显HTML代码。
使用完成:功能和不成功:功能
如果您想将其POST到同一个文件,请将URL保留为空