0

I have a form field (email signup) on the site, and the email provider wants me to submit it to their REST web service and get a response. I've never used JSON or AJAX before so floundering!

The HTML:

<form>
  <input type="hidden" name="gid" value="12345678">
  <input type="hidden" name="user.CustomAttribute.NewsletterPopUp" value="Global">
  <input type="hidden" name="user.CustomAttribute.NewsletterOptIn" value="True">" value="True">
  <input type="text" name="uemail" class="email_input_field" value="please enter your email" size="30" maxlength="64" onFocus="clearText(this)">
  <input type="submit" name="signup" value="signup" class="email_submit_button">
</form>

Currently, using Javascript and using window.location to visit the URL (which creates the action instead of posting it) they want it converted to a form post action with XML response. What happens now:

$(".email_submit_button").click(function(){
    var uemail = $('.email_input_field').val();
    window.location = "http://example.com/automated/action.jsp?action=register&errorPage=/automated/action.jsp&gid=12345678&uemail="+uemail+"&user.CustomAttribute.NewsletterPopUp=Global&user.CustomAttribute.NewsletterOptIn=True";
    return false;
  }
});
4

2 に答える 2

1

jQuery を使用しているようですので、$.post を使用して次のようにサーバーに投稿できます。

var url = "http://example.com/automated/action.jsp"
var data ={
    "gid": form.gid,
    "action": register,
    "uemail": form.uemail,
    "errorPage": "/automated/action.jsp",
    "user.CustomAttribute.NewsletterOptIn": user.CustomAttribute.NewsletterOptIn,
    "user.CustomAttribute.NewsletterPopUp": user.CustomAttribute.NewsletterPopUp
};
var success_func = function(data){
    //do what you want with the returned data
};
$.post(url, data, success_func);

$.post のドキュメント
または、$.post のドキュメントに記載されている、より長い純粋な Ajax バージョンを使用することもできます。
編集:
JSONP を使用する必要がある別のドメインに対して xhttpresuext を実行できないことを忘れています。すべてを詳細に説明する別の SO 投稿
へのリンク です。

于 2013-03-18T16:21:20.793 に答える
0
$(".email_submit_button").submit(function(e) {
                // stop form from submitting
                e.preventDefault();
                // Grab all values 
                var uemail = $('.email_input_field').val();
                // make a POST ajax call 
                $.ajax({
                    type: "POST",
                    url: "YOUR URL", // set your URL here
                    data: { 
                    uemail: uemail // send along this data (can add more data separated by comma)
                },
                beforeSend: function ( xhr ) {
                // maybe tell the user that the request is being processed
                    $("#status").show().html("<img src='images/preloader.gif' width='32' height='32' alt='processing...'>");
                }
                }).done(function( response ) {
                    // do something with the received data/response
                    //$("#status").html(response);
                });
            });

「.email_submit_button」が送信ボタンまたはフォームに指定されたクラスであるかどうかわかりません..送信ボタンではなく、フォームに指定されたIDまたはクラスを使用する必要があります..これが役立つことを願っています

于 2013-03-18T16:28:22.213 に答える