2013-04-09 7 views
0

다음은 잘 작동하는 링크입니다. 그것은 구글 API에서 Gmail 연락처를 가져옵니다 : 나는 div 버튼을 통해이 기능을 반복하려고시뮬레이션 <a> jquery를 통한 href 게시

<a id='gmailInvite' class="button icon chat" href="<?php echo site_url('main/gmail_invite'); ?>"><span>Import GMail Contacts</span></a> 

연결합니다.

사업부 버튼

<div class="gmailbutton" onclick="importGMailContacts()"><span class="textwhite">Import Gmail Contacts</span></a> 

function importGMailContacts() 
    { 
     var form_data = ""; 
     $.ajax({ 
       url: "<?php echo site_url('main/gmail_invite'); ?>", 
       type: 'POST', 
       data: form_data, 
       success: function(msg) { 
        alert(msg) 
        return true; 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        alert(xhr.status); 
        alert(xhr.responseText); 
        alert(thrownError); 
       } 
     }); 
    } 

나는이 방법을 컨트롤러로가는 DIV 버튼을 클릭합니다. 그러나 아무 일도 일어나지 않습니다. div 버튼에서 href post을 어떻게 시뮬레이트 할 수 있습니까?

+0

데이터를 form_data로 설정하는 것은 비어 있습니다. . . 그게 의도적 인거야? – ernie

+0

예. 그냥 그 방법을 치고 싶다. –

답변

1

혼란스러워 보입니다. A 요소를 클릭하면 POST가 아닌 GET 요청이 전송됩니다. 링크를 클릭하여 시뮬레이션하려면 window.location = "<?php echo site_url('main/gmail_invite'); ?>"을 사용합니다. 브라우저가 따르는 POST를 보내려면 적절한 조치로 FORM을 작성하여 제출해야합니다.

+0

고마워요. 나는 실제로 피곤하고 혼란 스럽다. –

0

JQuery "클릭"기능을 사용하면 요소를 조작하고 GET, POST 또는 사용자 지정 동작 일 수있는 모든 작업을 수행 할 수 있습니다. 예를 들어, 클릭 기능은 양식을 제출하거나 "링크"처럼 행동하거나 단순히 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); 

}); 

});