AJAX $_POST is not working
AJAX $_POST is not working
所以我想在单击按钮时在javascript文件和php文件之间传递一个值。但是当我点击按钮时,我没有看到回声,有人看到问题了吗?以下是我的两个代码片段:
test.php:
<script type="text/javascript" src="/functions/js/test.js"></script>
<button type="button" class="btn btn-success" onclick=" myAjax();">Test</button>
<?php
if($_POST['action'] == 'go')
{
echo "worked";
}
?>
和test.js
function myAjax() {
$.ajax({
type: "POST",
url: 'test.php',
data:{action:'go'},
success:function() {
}
});
}
首先需要包含jQuery库
第二,加载文件test.js 的路径
您应该使用一个类,而不是调用函数myAjax()。
在按钮中使用类,例如:测试
好吧,在你的test.js文件中使用这个:
$(document).on('click', '.Test', function(e) {
$.ajax({
type: "POST",
url: 'test.php',
data:{action:'go'},
success:function(data) {
console.log('worked');
}
});
});
如果只需要发送一个参数,使用这个代码,会更容易:
$(document).on('click', '.Test', function(e){
var vars = 'go';
var params='go='+vars;
location.href = 'test.php'+params;
});
然后在php文件中,您可以使用$_POSt或$_REQUEST来获取vars
<?php
if($_POST['action'] == 'go')
{
echo "worked";
}
?>
祝你今天愉快
您的问题是收到了响应,但没有输出。
使用console.log
向控制台输出:
$.ajax({
type: "POST",
url: 'test.php',
data:{action:'go'},
success: function(data) {
console.log(data);
}
});
您将在成功函数内部工作
function myAjax() {
$.ajax({
type: "POST",
url: 'test.php',
data:{action:'go'},
success:function(data) {
alert(data);//or console.log(data);
}
});
}
相关文章:
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working