Javascript 单击div时在jQuery中发送POST请求
我很难弄清楚如何完成以下任务Javascript 单击div时在jQuery中发送POST请求,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我很难弄清楚如何完成以下任务 单击段落元素 使用jQuery发送.post请求 从服务器获取要在段落中显示的已发送数据 我已经搜索了很多,并尝试使用一些提出的解决方案,但失败了 以下是我的文件夹结构: + html - index.html + js - eventhandling.js + php - test.php HTML 单击事件成功,因为会弹出警报。Firebug控制台窗口上没有显示任何内容。可能是您传递到post的服务URL错误(根据文件夹结构)。 $
- 单击段落元素
- 使用jQuery发送.post请求
- 从服务器获取要在段落中显示的已发送数据
+ html
- index.html
+ js
- eventhandling.js
+ php
- test.php
HTML
单击事件成功,因为会弹出警报。Firebug控制台窗口上没有显示任何内容。可能是您传递到post的服务URL错误(根据文件夹结构)。
$(".channel").click(function(){
/*alert(I am clicked");*/
$.post('test.php', {name: 'John'}, function(data) {
$("#demodata").text(data);
});
});
代码必须是这样的
$(".channel").click(function(){
var postData = {"name":"john"};
$.ajax({
type: "POST",
url: '../php/test.php',
data: postData ,
contentType: "application/json",
dataType: "json",
processdata: true,
success: function (response) {
},
error: function(error){
}
});
});
这对您很有用。控制台中实际上什么也没发生?试试$(“#demodata”).html(数据)@接下来是ajax:@WesleyMurch,但他使用的是val()方法而不是html()。P标记没有值属性。@JayBhatt就是这样。仍然应该是控制台中的一个请求。@WesleyMurch它是一个变量。“data”将始终将文本设置为文本字符串“data”。对,它应该可以工作并在div中打印“data”,我错过的部分是OP used
val()
。
$(".channel").click(function(){
/*alert(I am clicked");*/
$.post('test.php', {name: 'John'}, function(data) {
$("#demodata").val("data");
});
});
$(".channel").click(function(){
/*alert(I am clicked");*/
$.post('test.php', {name: 'John'}, function(data) {
$("#demodata").text(data);
});
});
$(".channel").click(function(){
var postData = {"name":"john"};
$.ajax({
type: "POST",
url: '../php/test.php',
data: postData ,
contentType: "application/json",
dataType: "json",
processdata: true,
success: function (response) {
},
error: function(error){
}
});
});