从另一个jquery调用带有响应的函数作为JSON数据并获得响应

时间:2014-04-02 05:14:55

标签: jquery ajax json.net

我想以JSON格式将数据库查询结果传递给jquery,因为我正在考虑这个问题 第一个函数将从某个c#函数中获取数据,然后将该数据作为输入传递给另一个jquery,以便在webservice上进行处理。

我是新手。有人可以帮助我

第一个功能:

 <script type="text/javascript">

var Result1;
var Result2;

function btnJquery_Click() {
    $.ajax({ type: "POST",
        url: "Default.asmx/GetData", // this will get data from c# function
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function CallanotherFunction (response) { Result1= JSON.parse(response);} 
    }); 

function CallanotherFunction () {
    $.ajax({ type: "POST",
        url: "Default.asmx/GetData", // this will get data from c# function
        data: Result1,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function CallanotherFunction (response) {Result2= JSON.parse(response); } 
    }); 

     }
</script>

这只是虚构的代码;不会工作..请帮我代码

1 个答案:

答案 0 :(得分:1)

尝试使用.done()

function btnJquery_Click() {
    $.ajax({ type: "POST",
        url: "Default.asmx/GetData", // this will get data from c# function
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) { 
        Result1= JSON.parse(response);

        }).done(function(Result1){
         CallanotherFunction (Result1)
        }) 
    }); 

OR

$.ajax({
    type: "POST",
    url: "Default.asmx/GetData", // this will get data from c# function
    data: {},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        Result1 = JSON.parse(data); //1st ajax data
        $.ajax({
            type: "POST",
            url: "Default.asmx/GetData", // this will get data from c# function
            data: Result1, //used 1st ajax data
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                Result2 = JSON.parse(response);//here 2nd ajax data based on first ajax call
            }
        });
    }
});