简体   繁体   中英

How do I send data to php on $.ajax success?

This could be a silly question, but I´m not used to working with Ajax and JSON. I have an array tags, and I want to get media_counts for them from Instagram. On success, I want to send the received data to a php-file (and later save it to a database table). I have this code:

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

You should

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

        }
    });   

}

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');
        }
        });

All fields are getting in POST methods in xyz.php php files

You can use json_encode() to communicate with jquery script.

For example

$('.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');
        }
    });
});

and php file (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'));
}

?>

By generalizing,

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

For Your requirement, eg.

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);

In success2 you send ajax request with data as content2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM