简体   繁体   English

如何使用JSON或AJAX将表单字段值发送到REST服务

[英]How to send form field value to a REST service using JSON or AJAX

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. 我在网站上有一个表单字段(电子邮件注册),并且电子邮件提供商希望我将其提交到他们的REST Web服务并获得响应。 I've never used JSON or AJAX before so floundering! 我从来没有使用过JSON或AJAX,然后才陷入困境!

The HTML: 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. 当前,他们希望使用Javascript并使用window.location访问URL(这将创建操作而不是发布操作),他们希望将其转换为带有XML响应的表单发布操作。 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;
  }
});

I see you'r using jQuery so you can use the $.post to post to the server like this: 我看到您使用的是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);

Documentation for $.post . $ .post的文档
Or you can use the pure longer Ajax version it's mentioned in the documentation of the $.post. 或者,您可以使用$ .post文档中提到的纯较长的Ajax版本。
EDIT: 编辑:
I forget you can't do xhttpresuext to a different domain you need to use JSONP, here's a link to another SO post explaining everything by detail 我忘记了您无法将xhttpresuext扩展到使用JSONP所需的其他域,这是指向另一篇SO帖子的链接,详细解释了所有内容
Hope this help. 希望能有所帮助。

$(".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);
                });
            });

Not sure if ".email_submit_button" is the class given to the submit button or the form.. you need to use the id or class given to the form and not the submit button.. hope this helps 不确定“ .email_submit_button”是提供给提交按钮或表单的类..您需要使用提供给表单的id或类而不是提交按钮..希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM