[英]PHP ajax post data empty
我要從create發布表單數據。 如下使用$.ajax
php
。
$(document).ready(function () {
var request;
$("#create-pto").submit(function(e) {
// Abort any pending request
if (request) {
request.abort();
}
var $form = $(this);
var serializedData = $form.serialize();
request = $.ajax({
url: 'index.php',
type: 'POST',
contentType : 'application/json',
data: serializedData
}).done(function (response, textStatus, jqXHR) {
// Log a message to the console
console.log("Hooray, it worked!");
});
e.preventDefault();
});
});
我可以看到發布的數據,也可以記錄發布成功。
但是在index.php中無法檢索發布的數據。
這就是我在index.php中嘗試的
var_dump($_POST);
形式:
<form class="form-horizontal" id="create-pto" >
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control- label">App ID</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="app-id" id="app-id" placeholder="App ID">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">App Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="app-name" id="app-name"
placeholder="App Name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Instance Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="inputPassword3" name="ins-name"
placeholder="Instance Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="create" class="btn btn-success">Create PTO</button>
</div>
</div>
</form>
請幫助。
嘗試更改JavaScript代碼上的contentType:
contentType : 'application/x-www-form-urlencoded',
而且,如果要將contentType保留為json
,請在您的PHP文件上嘗試以下方式:
echo file_get_contents('php://input');
如果使用.serialize()
,則需要將contentType
更改為“ application / x-www-form-urlencoded”。
如果要保留contentType: 'application/json'
,則必須使用JSON.stringify。
還要注意,POST請求中的JSON字符串使用file_get_contents('php://input');
檢索file_get_contents('php://input');
不是$_POST
改變你的
contentType:'application / json'
至
dataType:“ JSON”
它將開始工作。我在系統上測試了代碼。
嘗試使用type:GET
而不是type:POST
,也可以在index.php
調用var_dump($ _ GET)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.