通过AJAX提交值并返回值

发布于 2024-12-25 13:36:11 字数 1247 浏览 0 评论 0原文

我遇到了一点困难。基本上,我通过 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 以获取它回到我的应用程序中,因为我正在以“未定义”的方式做事!

谢谢 罗里

附注我正在使用 JSONP,因为我正在处理单独域上的脚本。

纯文本 JSON -

({"orderId":125})

I am having a little difficulty. Basically I am sending information from a form via jQuery and AJAX to MySQL, then returning a value back to my application.

I am able to add the information to my DB but I cant seem to work out how to retrieve the ID of the data inserted. Here is my code -

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

This sends the information fine. I am able to add to the DB and using the following to return the ID

$orderId = mysql_insert_id();

I then create JSON format for this value,

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

When I view this in FireBug, I can see the ID, What I need guidance is how to handle this ID to get it back into my application, As I am getting 'undefined' doing things my way!

Thanks
Rory

p.s. I am using JSONP as I am dealing with scripts on a separate domain.

Plain text JSON -

({"orderId":125})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2025-01-01 13:36:11

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

The ajax call is asynchronous. That means the ajax call completes and the following functions in your code continue to execute while the ajax call is underway and before it completes. Therefore, the ONLY place you can do anything with the return value is in the success handler for the ajax function or in any functions you call from there.

This requires changing the flow of your coding, but you need to make the ajax call and then continue the flow of execution in the success handler once the ajax call successfully completes.

Conceptually, it's like this:

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
屌丝范 2025-01-01 13:36:11

您应该修改您的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
    }
);

You should modify your submitDeliveryDetails function to take success and error callbacks, then invoke those from the jQuery ajax success and error:

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

Then call it:

submitDeliveryDetails({
    success: function(order) {
       alert(order.orderId);
    },
    error: function(xhr, status, err) {
       // Do Stuff
    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文