Php 模拟<;a>;href通过jquery发布

Php 模拟<;a>;href通过jquery发布,php,jquery,html,codeigniter,Php,Jquery,Html,Codeigniter,这是一个运行良好的链接。它从谷歌API导入GMail联系人: 链接 <a id='gmailInvite' class="button icon chat" href="<?php echo site_url('main/gmail_invite'); ?>"><span>Import GMail Contacts</span></a> 当我单击div按钮时,它转到controller方法。但什么也没发生。 如何从div按钮模拟hre

这是一个运行良好的链接。它从谷歌API导入GMail联系人:

链接

<a id='gmailInvite' class="button icon chat" href="<?php echo site_url('main/gmail_invite'); ?>"><span>Import GMail Contacts</span></a>
当我单击div按钮时,它转到controller方法。但什么也没发生。
如何从div按钮模拟
href post

您似乎感到困惑。单击元素将发送GET请求,而不是POST。要模拟单击链接,可以使用
window.location=”“
。要发送浏览器跟随的帖子,您需要使用适当的操作创建一个表单并提交它。

只需使用JQuery的“单击”功能,这样您就可以操作任何元素并执行任何需要执行的操作(可以是GET、POST或自定义操作)。例如,可以使用click函数提交表单、充当“链接”或简单地触发Ajax调用

以下是示例函数:

$('#gmailbutton').click(function(){

var form_data = 'WHATEVER DATA YOUR COLLECTING'
$.ajax({

    //the type of call it can be GET or POST 
    type:'POST',
    //If it is a static URL you dont need to echo with PHP 
    //just write it directly
    url:'main/gmail_invite/ajax.sample.call.php', //or whatever you php file is called

    //if you want to receive the data as html,json,xml
    dataType:'json',
    cache:false,

    beforeSend:function(x){
        //use this only if the response is a JSON object    
        if(x && x.overrideMimeType) {
            x.overrideMimeType("application/j-son;charset=UTF-8");
        }       

    }
}).done(function(response){

    //use "done" rather than "success", as "success" has been deprecated from JQUERY
    console.log( response );
    //no need to return, the ajax returns the call by default

}).fail(function(jqXHR, textStatus, errorThrown){

    console.log( jqXHR );
    console.log( textStatus );
    console.log( errorThrown );

});

}))

您正在将数据设置为表单_数据,该数据为空。这是故意的吗?是的。我只是想试试这个方法。非常感谢。我真的很累,很困惑。
$('#gmailbutton').click(function(){

var form_data = 'WHATEVER DATA YOUR COLLECTING'
$.ajax({

    //the type of call it can be GET or POST 
    type:'POST',
    //If it is a static URL you dont need to echo with PHP 
    //just write it directly
    url:'main/gmail_invite/ajax.sample.call.php', //or whatever you php file is called

    //if you want to receive the data as html,json,xml
    dataType:'json',
    cache:false,

    beforeSend:function(x){
        //use this only if the response is a JSON object    
        if(x && x.overrideMimeType) {
            x.overrideMimeType("application/j-son;charset=UTF-8");
        }       

    }
}).done(function(response){

    //use "done" rather than "success", as "success" has been deprecated from JQUERY
    console.log( response );
    //no need to return, the ajax returns the call by default

}).fail(function(jqXHR, textStatus, errorThrown){

    console.log( jqXHR );
    console.log( textStatus );
    console.log( errorThrown );

});