如何使用 jquery 对 php 文件进行 onclick ajax 调用? [英] How to do an onclick ajax call to a php file, with jquery?

查看:22
本文介绍了如何使用 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 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆