Javascript 如何将参数从链接传递到jquery方法
当用户单击链接时,我需要使用JQuery方法,并调用位于Javascript 如何将参数从链接传递到jquery方法,javascript,jquery,Javascript,Jquery,当用户单击链接时,我需要使用JQuery方法,并调用位于/Project/Send/SendMethod的web服务。当我点击send链接时,方法被触发,我知道这一点,因为警报正在显示。但问题是如何调用Web服务。如果这是一种POST方法就好了 <a href='' id='2' class='send'> Send </a> 我会使用jQuery的ajax()功能(并且经常这样做) 下面的示例假设您将得到一个JSON格式的响应。如果您要返回完整的HTML页面,可以更
/Project/Send/SendMethod
的web服务。当我点击send
链接时,方法被触发,我知道这一点,因为警报正在显示。但问题是如何调用Web服务。如果这是一种POST
方法就好了
<a href='' id='2' class='send'> Send </a>
我会使用jQuery的
ajax()
功能(并且经常这样做)
下面的示例假设您将得到一个JSON格式的响应。如果您要返回完整的HTML页面,可以更改此选项。。。查看更多信息
$(function () {
$('.send').click(function (e) {
e.stopPropagation();
e.preventDefault();
var thisId = $(this).attr('id');
alert(thisId);
var hostAndPort = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port; // current page and protocol (http or https)
var requestUrl = hostAndPort + "/Project/Send/SendMethod/";
var str_address = requestUrl + 'id?=' + thisId;
$.ajax({
url: str_address,
contentType: 'application/json',
dataType: 'JSON',
type: 'POST',
success: function (response) {
console.log(response);
// do something...
},
error: function (e) {
// handle error
}
});
});
});
我会使用jQuery的
ajax()
功能(并且经常这样做)
下面的示例假设您将得到一个JSON格式的响应。如果您要返回完整的HTML页面,可以更改此选项。。。查看更多信息
$(function () {
$('.send').click(function (e) {
e.stopPropagation();
e.preventDefault();
var thisId = $(this).attr('id');
alert(thisId);
var hostAndPort = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port; // current page and protocol (http or https)
var requestUrl = hostAndPort + "/Project/Send/SendMethod/";
var str_address = requestUrl + 'id?=' + thisId;
$.ajax({
url: str_address,
contentType: 'application/json',
dataType: 'JSON',
type: 'POST',
success: function (response) {
console.log(response);
// do something...
},
error: function (e) {
// handle error
}
});
});
});
使用$.ajax()
方法并指定url和如下选项
使用$.ajax()
方法并指定url和如下选项
使用jQuery
$。post
:
$(function () {
$('.send').click(function () {
alert(this.id);
$.post(url, {'id':this.id}, function (response) {
//do the result oriented activities
});
});
});
使用jQuery
$。post
:
$(function () {
$('.send').click(function () {
alert(this.id);
$.post(url, {'id':this.id}, function (response) {
//do the result oriented activities
});
});
});
您可以在jQuery中使用
$.ajax()
api。此外,您必须避免链接中的默认行为。否则,您将更改页面,而不是发送ajax请求
$('.send').click(function (event) {
event.preventDefault();
$.ajax( {
url:"Project/Send/SendMethod",
type: "POST",
data: { "id": this.id },
success:function(data) {
alert(data);
}
});
});
如果您使用的是jQuery1.8+,因为从jQuery1.8开始就不推荐使用“success”回调。你应该使用“完成”
您可以在jQuery中使用
$.ajax()
api。此外,您必须避免链接中的默认行为。否则,您将更改页面,而不是发送ajax请求
$('.send').click(function (event) {
event.preventDefault();
$.ajax( {
url:"Project/Send/SendMethod",
type: "POST",
data: { "id": this.id },
success:function(data) {
alert(data);
}
});
});
如果您使用的是jQuery1.8+,因为从jQuery1.8开始就不推荐使用“success”回调。你应该使用“完成”
你需要阅读,然后自己在本地服务器上试用。你需要阅读,然后自己在本地服务器上试用。不要忘记
event.prevenDefault()
。不要忘记event.prevenDefault()
。那么如果我用/Project/Send/SendMethod
替换test.php
,它会工作吗?我试过了,但它没有调用Web服务。所以,如果我用/Project/Send/SendMethod
替换test.php
,它会工作吗?我试过了,但它不叫网络服务。