PHP ajax post数据为空
我正在使用PHP ajax post数据为空,php,jquery,ajax,Php,Jquery,Ajax,我正在使用$.ajax发布来自create.php的表单数据,如下所示 $(document).ready(function () { var request; $("#create-pto").submit(function(e) { // Abort any pending request if (request) { request.abort(); } var $form = $(
$.ajax
发布来自create.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>
应用程序ID
应用程序名称
实例名
创建PTO
请提供帮助。尝试更改JavaScript代码的内容类型:
contentType:'application/x-www-form-urlencoded',
如果要将contentType保持为json
,请在PHP文件上尝试以下方法:
echo文件\u获取\u内容('php://input');代码>尝试更改JavaScript代码的内容类型:
contentType:'application/x-www-form-urlencoded',
如果要将contentType保持为json
,请在PHP文件上尝试以下方法:
echo文件\u获取\u内容('php://input');
如果使用.serialize()
则需要将contentType
更改为“application/x-www-form-urlencoded”
如果要保留contentType:'application/json'
,则必须改用json.stringify
还要注意,POST请求中的JSON字符串是使用file\u get\u contents('php://input');如果使用.serialize()
,则需要将内容类型
更改为“application/x-www-form-urlencoded”
如果要保留contentType:'application/json'
,则必须改用json.stringify
还要注意,POST请求中的JSON字符串是使用file\u get\u contents('php://input');代码>不$\u POST
更改您的
contentType:'应用程序/json'
到
数据类型:“JSON”
它将开始工作。。我在我的系统上测试了代码。更改您的
contentType:'应用程序/json'
到
数据类型:“JSON”
它将开始工作。。我在我的系统上测试了代码。尝试type:GET
而不是type:POST
,也可以在index.php
中调用var\u dump($\u-GET)尝试type:GET
而不是type:POST
,也可以在index.php
中调用var\u-dump>您的表单代码?当您使用web工具Browser观看“网络”时,可以吗?#创建pto是表单id还是按钮id?#创建pto是表单id表单何时提交,没有任何提交按钮?您可以发布您的表单代码吗?请发布您的表单代码?当您使用web工具浏览器观看您的“网络”时,可以吗?#创建pto是表单id还是按钮id?#创建pto是表单id,表单将提交时,没有任何提交按钮?您可以发布表单代码吗?您应该使用JSON.stringify(serializedData)发送JSON类型的数据。我试过contentType:'application/x-www-form-urlencoded',data:serializedData和var\u dump($\u REQUEST)我在index.php中得到了var\u dump($\u REQUEST)的数组(0){};您应该使用JSON.stringify(serializedData)发送JSON类型的数据。我试过contentType:'application/x-www-form-urlencoded',data:serializedData和var\u dump($\u REQUEST)我在index.php中得到了var\u dump($\u REQUEST)的数组(0){};我将contentType改为“application/x-www-form-urlencoded”。并尝试了文件获取内容('php://input'); 但“回声”不会在索引上打印任何内容。php@Spdexter尽量不要序列化或串接数据,并检查$\u POST
是否已被填充。我已将contentType:更改为“application/x-www-form-urlencoded”。并尝试了文件获取内容('php://input'); 但“回声”不会在索引上打印任何内容。php@Spdexter尽量不要序列化或串接数据,并检查是否填充了$\u POST