如何在$ .ajax成功时向php发送数据?

时间:2013-09-27 09:33:10

标签: php json jquery

这可能是一个愚蠢的问题,但我不习惯使用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
        }
    });     
}   

4 个答案:

答案 0 :(得分: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
        }
    });    

        }
    });   

}

答案 1 :(得分: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 php文件中的POST方法中

答案 2 :(得分: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'));
}

?>

答案 3 :(得分: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中,您发送ajax请求,其数据为content2