Javascript 使用从php接收的JS解析json数据
我的表格:Javascript 使用从php接收的JS解析json数据,javascript,json,ajax,Javascript,Json,Ajax,我的表格: <form class="form-inline signup" action="php/signupForm.php" role="form" id="signupForm"> <div class="form-group"> <input type="email" name="email" class="form-control" placeholder="Email address">
<form class="form-inline signup" action="php/signupForm.php" role="form" id="signupForm">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email address">
</div>
<div class="form-group">
<button type="submit" class="btn btn-theme ladda-button" data-style="expand-left">
<span class="ladda-label" id="notice">Get notified!</span>
</button>
</div>
</form>
我的页面正在接收以下数据:
{"status":0,"message":"This email is already on list!"}
使用JS,我需要解析数据,然后更新元素中的文本
<span id="notice">Get notified!</span>
您必须在回调中处理响应
$(document).ready(function() {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
data: $(this).serialize(),
url: $(this).attr('action'), // Or the path of the PHP file
dataType: 'json',
}).done(function(response) {
$('#notice').text(response.message);
});
});
});
请参阅相关文档您必须在回调中处理响应
$(document).ready(function() {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
data: $(this).serialize(),
url: $(this).attr('action'), // Or the path of the PHP file
dataType: 'json',
}).done(function(response) {
$('#notice').text(response.message);
});
});
});
请参阅相关文档您必须在回调中处理响应
$(document).ready(function() {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
data: $(this).serialize(),
url: $(this).attr('action'), // Or the path of the PHP file
dataType: 'json',
}).done(function(response) {
$('#notice').text(response.message);
});
});
});
请参阅相关文档您必须在回调中处理响应
$(document).ready(function() {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
data: $(this).serialize(),
url: $(this).attr('action'), // Or the path of the PHP file
dataType: 'json',
}).done(function(response) {
$('#notice').text(response.message);
});
});
});
请参阅相关文档,ajax调用的格式不正确,缺少成功回调和url,例如:
$(document).ready(function () {
$.ajax({
url: '/the/url/where/your/data/comes/from/',
dataType: 'json',
success: function (data) {
$('#notice').text(data.message);
}
});
});
该ajax调用格式不正确,缺少成功回调和url,例如:
$(document).ready(function () {
$.ajax({
url: '/the/url/where/your/data/comes/from/',
dataType: 'json',
success: function (data) {
$('#notice').text(data.message);
}
});
});
该ajax调用格式不正确,缺少成功回调和url,例如:
$(document).ready(function () {
$.ajax({
url: '/the/url/where/your/data/comes/from/',
dataType: 'json',
success: function (data) {
$('#notice').text(data.message);
}
});
});
该ajax调用格式不正确,缺少成功回调和url,例如:
$(document).ready(function () {
$.ajax({
url: '/the/url/where/your/data/comes/from/',
dataType: 'json',
success: function (data) {
$('#notice').text(data.message);
}
});
});
您的代码只是在页面加载时执行,而不是在表单提交时执行。您需要附加onsubmit事件,防止执行表单提交的默认操作,并在其中执行ajax调用。另外,ajax调用本身的格式也不正确
$("#yourFormID").submit(function(e){
e.preventDefault();
$.ajax({
url:"/urlToServerScript",
data:{} //any form data the script needs you should be put here,
dataType:"json" //type of response the server will output
}).then(function(data){
$('#notice').text(data.message);
});
});
您的代码只是在页面加载时执行,而不是在表单提交时执行。您需要附加onsubmit事件,防止执行表单提交的默认操作,并在其中执行ajax调用。另外,ajax调用本身的格式也不正确
$("#yourFormID").submit(function(e){
e.preventDefault();
$.ajax({
url:"/urlToServerScript",
data:{} //any form data the script needs you should be put here,
dataType:"json" //type of response the server will output
}).then(function(data){
$('#notice').text(data.message);
});
});
您的代码只是在页面加载时执行,而不是在表单提交时执行。您需要附加onsubmit事件,防止执行表单提交的默认操作,并在其中执行ajax调用。另外,ajax调用本身的格式也不正确
$("#yourFormID").submit(function(e){
e.preventDefault();
$.ajax({
url:"/urlToServerScript",
data:{} //any form data the script needs you should be put here,
dataType:"json" //type of response the server will output
}).then(function(data){
$('#notice').text(data.message);
});
});
您的代码只是在页面加载时执行,而不是在表单提交时执行。您需要附加onsubmit事件,防止执行表单提交的默认操作,并在其中执行ajax调用。另外,ajax调用本身的格式也不正确
$("#yourFormID").submit(function(e){
e.preventDefault();
$.ajax({
url:"/urlToServerScript",
data:{} //any form data the script needs you should be put here,
dataType:"json" //type of response the server will output
}).then(function(data){
$('#notice').text(data.message);
});
});
什么url会出现,它会从服务器返回到页面?在浏览器调试器的“网络”选项卡中调用的PHP脚本的url,因此您看到调用被触发?我猜您没有在
url
参数中引用正确的php文件参见我更新的anwser,它详细说明了如何处理调用本身。对于GET
vsPOST
,这将影响您在php脚本中使用数据的方式,我建议您为此使用POST
,您将能够在$\u POST
数组中操作PHP脚本中发布的数据。哪个url会出现,它会从服务器返回到页面?您在浏览器调试器的“网络”选项卡中调用的PHP脚本的url,因此您看到您的调用被触发?我猜您没有在url
参数中引用正确的php文件参见我更新的anwser,它详细说明了如何处理调用本身。对于GET
vsPOST
,这将影响您在php脚本中使用数据的方式,我建议您为此使用POST
,您将能够在$\u POST
数组中操作PHP脚本中发布的数据。哪个url会出现,它会从服务器返回到页面?您在浏览器调试器的“网络”选项卡中调用的PHP脚本的url,因此您看到您的调用被触发?我猜您没有在url
参数中引用正确的php文件参见我更新的anwser,它详细说明了如何处理调用本身。对于GET
vsPOST
,这将影响您在php脚本中使用数据的方式,我建议您为此使用POST
,您将能够在$\u POST
数组中操作PHP脚本中发布的数据。哪个url会出现,它会从服务器返回到页面?您在浏览器调试器的“网络”选项卡中调用的PHP脚本的url,因此您看到您的调用被触发?我猜您没有在url
参数中引用正确的php文件参见我更新的anwser,它详细说明了如何处理调用本身。对于GET
vsPOST
,这将影响您在php脚本中使用数据的方式,我建议您为此使用POST
,您将能够在$\u POST
阵列中操作PHP脚本中发布的数据。如果您得到响应屏幕,那么您将执行常规表单提交,而不是阻止表单提交(即在onsubmit事件中使用preventDefault),然后执行ajax调用。有了列出的代码,您将在主页加载后立即执行ajax调用,而不是在您提交FORM时。我认为您是对的,我认为我以前已经这样做过。那么要修复我是否执行ajax onclick submit?如果您得到一个响应屏幕,那么您是在执行常规表单提交,而不是阻止表单提交(即在onsubmit事件中使用preventDefault),然后执行ajax调用。有了列出的代码,您将在主页加载后立即执行ajax调用,而不是在您提交FORM时。我认为您是对的,我认为我以前已经这样做过。那么要修复我是否执行ajax onclick submit?如果您得到一个响应屏幕,那么您是在执行常规表单提交,而不是阻止表单提交(即在onsubmit事件中使用preventDefault),然后执行ajax调用。有了列出的代码,您将在主页加载后立即执行ajax调用,而不是在您提交FORM时。我认为您是对的,我认为我以前已经这样做过。那么要修复我是否执行ajax onclick submit?如果您得到一个响应屏幕,那么您是在执行常规表单提交,而不是阻止表单提交(即在onsubmit事件中使用preventDefault),然后执行ajax调用。有了列出的代码,您将在主页加载后立即执行ajax调用,而不是在您提交FORM时。我认为您是对的,我认为我以前已经这样做过。那么,为了解决这个问题,我需要执行ajax onclick submit吗?但是数据已经存在了