通过AJAX提交值并返回值

时间:2012-01-08 17:02:39

标签: jquery ajax json jsonp

我有点困难。基本上我是通过jQuery和AJAX将表单中的信息发送到MySQL,然后将值返回给我的应用程序。

我能够将信息添加到我的数据库,但我似乎无法找出如何检索插入数据的ID。这是我的代码 -

function submitDeliveryDetails(){
    $.ajax({                       
        url: "http://www.mydomain.co.uk/mobileapp/add_deliverydetails.php?callback=jsonp1",
        data: addDeliveryData, // Variable holding said delivery information
        jsonp: "callback",
        dataType: "jsonp",
        success: function(data) {
            $.each(data, function(index) {
                alert(data[index].orderId);
            });                       
        },                     
        error: function(){                       
            //Do Stuff                       
        }
    })
}

这样可以很好地发送信息。我可以添加到数据库并使用以下内容返回ID

$orderId = mysql_insert_id();

然后我为这个值创建JSON格式,

$orderIdArray = array('orderId'=>$orderId);
echo $_GET['callback'].'('.json_encode($orderIdArray).')';

当我在FireBug中查看此内容时,我可以看到ID,我需要的指导是如何处理此ID以将其恢复到我的应用程序中,因为我正在获取' undefined'按我的方式做事!

由于 罗里

P.S。我正在使用JSONP,因为我正在处理单独域上的脚本。

纯文本JSON -

({"orderId":125})

2 个答案:

答案 0 :(得分:1)

ajax调用是异步的。这意味着ajax调用完成,代码中的以下函数在ajax调用正在进行时以及完成之前继续执行。因此,您可以使用返回值执行任何操作的唯一位置是ajax函数的成功处理程序或您从那里调用的任何函数。

这需要更改编码流程,但是您需要进行ajax调用,然后在ajax调用成功完成后继续成功处理程序中的执行流程。

从概念上讲,就像这样:

function submitDeliveryDetails(){
    $.ajax({                       
        url: "http://www.mydomain.co.uk/mobileapp/add_deliverydetails.php?callback=jsonp1",
        data: addDeliveryData, // Variable holding said delivery information
        jsonp: "callback",
        dataType: "jsonp",
        success: function(data) {
            $.each(data, function(index) {
                alert(data[index].orderId);
            });                       
            // you can now continue on here with whatever you wanted to do 
            //    with the the returned JSON data
            // You can operate on the data here or call other functions 
            //    (passing the data as an argument to those functions)
        },                     
        error: function(){                       
            //Do Stuff                       
        }
    })
}

submitDeliveryDetails();
// nothing else to do here because the ajax call has not completed yet
// so the returned results from the ajax call is not available yet

答案 1 :(得分:0)

你应该修改你的submitDeliveryDetails函数来获取成功和错误回调,然后从jQuery ajax中调用那些成功和错误:

function submitDeliveryDetails(options){
    $.ajax({                       
        url: "http://www.mydomain.co.uk/mobileapp/add_deliverydetails.php?callback=jsonp1",
        data: addDeliveryData, // Variable holding said delivery information
        jsonp: "callback",
        dataType: "jsonp",
        success: function(data) {
            if (options && typeof(options.success) === 'function' ) options.success(data);
        },                     
        error: function(xhr, status, err){                       
            if (options && typeof(options.error) === 'function' ) options.error(xhr, status, err);
        }
    });
}

然后叫它:

submitDeliveryDetails({
    success: function(order) {
       alert(order.orderId);
    },
    error: function(xhr, status, err) {
       // Do Stuff
    }
);
相关问题