How to do an onclick ajax call to a php file, with jquery?(如何使用 jquery 对 php 文件进行 onclick ajax 调用?)
问题描述
我有一个链接,它链接到 domain.com ,当有人点击时,我希望它对 counter.php 进行 ajax 调用并向其发布 2 个变量,因此它可以在该链接的视图中添加 1.
I have a link, which links to domain.com , when a person clicks, I want it to do an ajax call to counter.php and post 2 variables to it, so it can add 1 to the views for that link.
我有一个链接:
<a href="http://www.domain.com" onClick="addHit('12345', '1')" rel="nofollow" target="_blank">Link Title</a>
我将如何使用 jquery 做到这一点?
How would I do this with jquery?
我试过这样的
function addHit(str, partNumber){
$.get("counter.php", { version: str, part: partNumber })
}
它似乎工作,但在萤火虫中,请求永远不会完成......它只是做那个工作......"动画.counter.php 完成后会回显一些文本(不需要出现在任何地方).
It seems to work, but in firebug, the request never completes... it just does that "working..." animation. counter.php echos out some text when its done (doesnt need to show up anywhere).
推荐答案
来自 jQuery 文档:http://api.jquery.com/jQuery.ajax/
From the jQuery documentation: http://api.jquery.com/jQuery.ajax/
function addHit(data1, data2)
{
$.ajax({
type: "POST",
url: "http://domain.com/counter.php",
data: "var1=data1&var2=data2",
success: function(msg){
alert( "Data Saved: " + msg ); //Anything you want
}
});
}
这篇关于如何使用 jquery 对 php 文件进行 onclick ajax 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 jquery 对 php 文件进行 onclick ajax 调用?
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
