在ASP。NET,如何在提交表单的同时处理客户端(javascript)和服务器端数据?

时间:2021-10-26 15:53:14

I have a scenario where I am using Google Map JavaScript API to process the address after user submits the form. However, I want to post result, that Google API returns back, to server side for further processing within the same form submission. How can I achieve this? Thanks in advance for your advice.

我有一个场景,使用谷歌映射JavaScript API在用户提交表单后处理地址。但是,我希望将谷歌API返回的结果发布到服务器端,以便在相同的表单提交中进行进一步处理。我如何做到这一点?谢谢你的建议。

1 个解决方案

#1


3  

Using jQuery, you could handle it like this:

使用jQuery,您可以这样处理:

$(document).ready(function() {
    $("form").submit(function(e) {
        // blocks form from being submitted, which you'll handle later
        e.preventDefault();

        // call google apis 

        // build data to submit 
        var data = {
            formData: $("something").val();
            // could use formData: $(this).serialize(); instead, 
            // more server-side parsing required though

            // add additional data from the google service to data object
        };

        // submit the form with serialized data
        $.post("/serverurl", data);
    });
});

If you're using Webforms, set the serverurl to a .ashx handler, parse the submitted data (will be set to Request.Form[] values). If you're using MVC, change the server url to a controller action and parse accordingly.

如果您正在使用Webforms,请将serverurl设置为.ashx处理程序,解析提交的数据(将被设置为Request。形式[]值)。如果您正在使用MVC,请将服务器url更改为控制器操作并相应地进行解析。

#1


3  

Using jQuery, you could handle it like this:

使用jQuery,您可以这样处理:

$(document).ready(function() {
    $("form").submit(function(e) {
        // blocks form from being submitted, which you'll handle later
        e.preventDefault();

        // call google apis 

        // build data to submit 
        var data = {
            formData: $("something").val();
            // could use formData: $(this).serialize(); instead, 
            // more server-side parsing required though

            // add additional data from the google service to data object
        };

        // submit the form with serialized data
        $.post("/serverurl", data);
    });
});

If you're using Webforms, set the serverurl to a .ashx handler, parse the submitted data (will be set to Request.Form[] values). If you're using MVC, change the server url to a controller action and parse accordingly.

如果您正在使用Webforms,请将serverurl设置为.ashx处理程序,解析提交的数据(将被设置为Request。形式[]值)。如果您正在使用MVC,请将服务器url更改为控制器操作并相应地进行解析。