[英]Wait for Form Submit before API call AJAX
Solution : No need to look for the PHP variables, just prevent default submission and use the Javascript variable in the API call.. 解决方案 :无需查找PHP变量,只需阻止默认提交并在API调用中使用Javascript变量即可。
I am attempting to use this workaround to wait for form submission before executing API call. 我正在尝试使用此替代方法来等待表单提交,然后再执行API调用。 However, the first ajax push is not resulting in the $_POST variable receiving the data.
但是,第一次ajax推送不会导致$ _POST变量接收数据。 Network analysis shows that the variable is being sent.
网络分析表明正在发送变量。 First question on here, so apologies in advance.
这里的第一个问题,所以提前致歉。
$("#form").submit(function(evt) {
evt.preventDefault();
var inputAddress = $(this).find("input[type='text']").val();
// Ajax form Submit
$.ajax ({
url:'index.php',
method: 'POST',
data:{inputAddress:inputAddress},
success: function() {
var add = '<?php echo ($_POST['inputAddress'])?>'; // BLANK?
// API call
var apiResult = $.ajax ({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+add+'&key=AIzaSyDcNLmq6E6K-bDefI-28E1qiqugS_-wnhI',
method: 'get',
success: function(data) {
window.console.log(data);
}
})
}
})
})
})
It seems like already have that variable on javascript, so there's no need to get it from $_POST. 似乎在javascript上已经具有该变量,因此无需从$ _POST获取它。 Try using this way and see if it works:
尝试使用这种方式,看看是否可行:
$("#form").submit(function(evt) {
evt.preventDefault();
var inputAddress = $(this).find("input[type='text']").val();
// Ajax form Submit
$.ajax ({
url:'index.php',
method: 'POST',
data:{inputAddress:inputAddress},
success: function() {
// API call
var apiResult = $.ajax ({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+inputAddress+'&key=AIzaSyDcNLmq6E6K-bDefI-28E1qiqugS_-wnhI',
method: 'get',
success: function(data) {
window.console.log(data);
}
})
}
})
})
})
EDIT: Also the first request is unnecessary, so you can remove it: 编辑:此外,第一个请求是不必要的,因此您可以将其删除:
$("#form").submit(function(evt) {
evt.preventDefault();
var inputAddress = $(this).find("input[type='text']").val();
// API call
var apiResult = $.ajax ({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+inputAddress+'&key=AIzaSyDcNLmq6E6K-bDefI-28E1qiqugS_-wnhI',
method: 'get',
success: function(data) {
window.console.log(data);
}
})
})
I found a problem in your code. 我在您的代码中发现了问题。 The page containing the above code is a PHP script , I think.
我认为包含以上代码的页面是一个PHP脚本 。
So, the Apache(PHP server) processes <?php echo ($_POST['inputAddress'])?>
and prints Empty value into it . 因此,Apache(PHP服务器)处理
<?php echo ($_POST['inputAddress'])?>
并在其中打印Empty值 。
$("#form").submit(function(evt) { evt.preventDefault(); var inputAddress = $(this).find("input[type='text']").val(); // Ajax form Submit $.ajax ({ url:'index.php', method: 'POST', data:{inputAddress:inputAddress}, success: function() { var add = ''; // PHP leaves it Empty... // API call var apiResult = $.ajax ({ url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+add+'&key=AIzaSyDcNLmq6E6K-bDefI-28E1qiqugS_-wnhI', method: 'get', success: function(data) { window.console.log(data); } }) } }) }) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.