1

1つのフォームをajaxなしで2つの異なる場所に送信するにはどうすればよいですか(クロスドメインの問題)

私は以下のようなことを考えていました。どのようにパラメータを渡すのでしょうか?

   < form action="urlOne.com/something" method="post" onsubmit="postToUrl("urlTwo.com/something");">
        <input type="text" value="hello" name="hi"/>
        <input type="submit" value="submit">
    </form>

ここから取得した関数>>フォーム送信のようなJavaScriptPOSTリクエスト

function postToUrl(url, params)

        {
            var form = $('<form>');
            form.attr('action', url);
            form.attr('method', 'POST');

            var addParam = function(paramName, paramValue){
                var input = $('<input type="hidden">');
                input.attr({ 'id':     paramName,
                             'name':   paramName,
                             'value':  paramValue });
                form.append(input);
            };

            // Params is an Array.
            if(params instanceof Array){
                for(var i=0; i<params.length; i++){
                    addParam(i, params[i]);
                }
            }

            // Params is an Associative array or Object.
            if(params instanceof Object){
                for(var key in params){
                    addParam(key, params[key]);
                }
            }

            // Submit the form, then remove it from the page
            form.appendTo(document.body);
            form.submit();
            form.remove();
        }

ありがとうございました。

4

3 に答える 3

4

You can only post one form at a time in a window. If you try to post two forms, one post will stop the other.

The solution would be to post the two forms to different windows. You can set the target of the first form to post to an iframe in the page or to a new window:

form.attr('target', '_blank');

This way the two posts would load in separate windows, and wouldn't stop each other.

于 2012-09-25T20:38:28.080 に答える
1

「見えない」投稿を別の URL に作成したいだけの場合...この場合、URL は Workout.php です。

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
  $('form').submit(function(data) {
    $.post('workout.php', $(this).serialize());
  });
});
</script>

<form action="urlOne.com/something" method="post">
        <input type="text" value="hello" name="hi"/>
        <input type="submit" value="submit">
</form>
于 2012-09-25T21:17:09.740 に答える
0

You can use crossdomain ajax with jQuery if you use a proxy server-side.

or:

Use apache's mod_rewrite or mod_proxy to pass requests from your server to some other server. In your client code you just make the request as if it was actually on your server -- no browser problems with that. Apache then does its magic and makes the request to the other server for you.

Here is a link to achieve this in php: http://developer.yahoo.com/javascript/howto-proxy.html#phpproxy

于 2012-09-25T20:38:32.957 に答える