我已經使用郵遞員發送json數據到指定的url.I得到reposured響應是郵政中的json。我想發送這個json到其他文件所以我用ajax post method.Ajax post方法沒有被調用。我的函數在這裏沒有觸發成功或錯誤。Ajax成功和錯誤函數沒有在jquery中正確調用
<?php
// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$input = json_decode(file_get_contents('php://input'),true);
$value1=json_encode($input);
//echo print_r($value1,1);
?>
<script type="text/javascript">
$(document).ready(function() {
// alert("hello");
var jsondata = <?php echo $value1 ?>;
$.ajax({
url: "/restapi/subscribe.php",
type: "POST",
data: { jsondata:jsondata },
dataType: "json",
Content-Type: "application/json",
success: function() {
alert("data sent!");
},
error: function() {
alert("There was an error. Try again please!");
}
});
return false;
});
</script>
我發送任何JSON數據作爲input.Output還JSON數據應在subscribe.php.In subscribe.php張貼我寫
<?php
$json=$_POST['jsondata'];
echo $json;
?>
也許這是因爲錯誤。你拼錯'contentType'。 'Content-Type'無效js – disstruct