2013-09-27 7 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 

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

     } 
    }); 

}

+3

가 왜'success' 콜백 안에 다른 아약스 요청을 발생하지 않는 content2 등의 데이터와 아약스 요청을 보내? – trrrrrrm

답변

0

:이 코드가 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

감사합니다! 하지만 어떻게 내 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'); 
     } 
     }); 

모든 항목이 xyz.php에 POST 방법에 점점 PHP 파일

0

당신은 json_encode()을 사용할 수 있습니다

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 
     } 
    });  
} 
+0

여기서 "양식"은 어디에서 왔습니까? 어떻게 든 $ _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, 
    }); 
} 

, 예.

success2에서
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); 

당신은

관련 문제