2013-09-27 39 views
0

這可能是一個愚蠢的問題,但我不習慣使用Ajax和JSON。我有一個數組標籤,我想從Instagram獲取media_counts。成功時,我想將收到的數據發送到一個php文件(並稍後保存到數據庫表)。我有這樣的代碼:

for (var i = 0; i < tags.length; i++) { 
    var tagname = tags[i]; 

    var url = "https://api.instagram.com/v1/tags/" + tagname + "?client_id=" + clientID; 
    $.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     cache: false, 
     url: url, 
     success: function (res) { 
      // Send res.data to php-file 
     } 
    });  
} 
+3

你爲什麼不火'success'回調內的另一個Ajax請求Ajax請求? – trrrrrrm

回答

0

你應該

for (var i = 0; i < tags.length; i++) { 
    var tagname = tags[i]; 

    var url = "https://api.instagram.com/v1/tags/" + tagname + "?client_id=" + clientID; 
    $.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     cache: false, 
     url: url, 
     success: function (res) { 
      // Send res.data to php-file 

     $.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     cache: false, 
     url: "2nd url", 
     success: function (res) { 
      //Do with res 
     } 
    });  

     } 
    }); 

}

+0

謝謝!但是,我如何在我的php文件中編碼?我只是在我嘗試的所有事情上得到「未定義索引」,所以我不知道是否有任何工作。 – tofu

+0

如果你正在推廣你的'AJAX'功能,它將會很容易! –

0
var Url = "http://xyz.com/xyz.php" 

var postData = $('selector').serialize(); 

    $.ajax({ 
     type: 'POST', 
     data: postData+'&FormType=PostVeriable', 
     url: Url, 
     success: function(data){ 
       //console.log(data); 
      //alert('There was an success'); 
     }, 
     error: function(){ 
      //console.log(data); 
      //alert('There was an error in register form'); 
     } 
     }); 

所有字段越來越在POST方法xyz.php PHP文件

0

您可以使用json_encode()與jQuery腳本進行通信。

例如

$('.submitbutton').click(function(event){ 

    event.preventDefault(); 

    $('.errormessage').html(''); 

    var frmdata = $('#form').serialize(); 

    $.ajax({ 
     url: '/ajax.php', 
     type: 'post', 
     dataType: 'json', 
     data: frmdata, 
     success: function(data){ 
      if(data.status === 'error'){ 
       $('.errormessage').html(data.message); 
      }else{ 
       $('.succesmeesage').html(data.message); 
      } 
     }, 
     error: function(){ 
      $('.errormessage').html('there is an error'); 
     } 
    }); 
}); 

和PHP文件(ajax.php)

<?php 

if(empty($_POST['form'])){ 
    echo json_encode(array('status'=>'error', 'message'=>'Form is empty')); 
}else{ 
    echo json_encode(array('status'=>'success', 'message'=>'Form is not empty')); 
} 

?> 
+0

「form」來自哪裏?不知怎的,我必須使用$ _POST ['data']?我對嗎? – tofu

+0

@tofu我編輯了我的答案。希望能幫助到你。 #form是您的表單ID –

0

通過推廣,

function ajaxified(destinationUrl,type, dataType, content, successFunction) 
{ 
    $.ajax({ 
     type: type, 
     url: destinationUrl, 
     data: content, 
     cache: true, 
     dataType: dataType, 
     success: successFunction, 
    }); 
} 

您的需求, 如。

var content1 = { 
    'tag' : '1', 
    'clientId' : '2' 
} 
var success1 = function(res){ 
    var content2 = res.data; 
    var success2 = function(data){/*some thing to do*/} 
    ajaxified('url2','GET', 'jsonp', content2, success2); 
} 
ajaxified('url1','GET', 'jsonp', content1, success1); 

在success2你發送的數據爲內容2