我正在尝试获取与jQuery AJAX一起传递给php页面的数据变量的值。如何解决这个问题?
下面是HTML页面代码:
<input id="name" placeholder="Enter your name." />
<button id="submit">Submit</button>
<div id="message"></div>在该按钮上,单击此jQuery AJAX调用一个php页面:
$.ajax({
type: "POST",
url: "jquery-ajax-hello.php",
contentType: "application/json; charset=utf-8",
data: '{"name":"' + $("#name").val() + '"}',
success: function (result, status, xhr) {
$("#message").html(result);
},
error: function (xhr, status, error) {
$("#message").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}
});PHP页面代码是:
<?php
$name = $_POST['name'];
echo "Hello ".$name.", How are you ?";
?>在php页面中,我无法获取数据变量“name”值?
请帮帮忙?
发布于 2017-02-02 10:03:12
您的data应该是一个对象,您要传递的是一个字符串,所以它应该是
data: {
name: $("#name").val()
},发布于 2017-02-02 10:09:25
JQuery Ajax
表格数据
<input type="text" id="name" placeholder="Enter your name." />
<button id="submit">Submit</button>
<div id="message"></div>Jquery部分
$(function() {
$("#submit").click(function() {
var name = $('#name').val();
$.ajax({
url : 'success.php',
method: 'POST',
data : {name:name},
success : function(res) {
$('#message').append(res);
}
});
});
});success.php
<?php
$name = $_POST['name'];
echo "Hello Mr ".$name ;
?>发布于 2017-02-02 10:02:56
将数据属性更改为
data: {name: $("#name").val() }而且效果很好
https://stackoverflow.com/questions/41999111
复制相似问题