如何将此表格放入AJAX请求中

时间:2018-07-20 11:38:52

标签: javascript html ajax api

我想将此HTML表单放入AJAX请求中,然后将响应保存在var中。我试图拦截提交,并提出了AJAX请求,该请求已经在其他主题中提出过。我只是在寻找一些解释,说明如何将所有“重要”代码放入我的AJAX请求中(例如grand_type等)

<!--   form for authentication   -->
    <form id="auth-form" action="https://rsgdeborgen.zportal.nl/api/v3/oauth/token" method="post">
    <!--   hidden post data      -->
    <input id="hidden-input" type="hidden" name="grant_type" value="authorization_code"/>
    <!--    user inputfield || user input = 12 number long authorization code from: https://rsgdeborgen.zportal.nl -->
    <input id="user-auth-input" name="code" type="text" placeholder="Koppelcode" minlength=12 maxlength=15 required autofocus/>
    <!--    post button    -->
    <button id="post-button" value="inloggen">inloggen</button>

这是如何计算我的AJAX的示例:

var body = {
    grant_type: 'authorization-code';
    authorization-code: document.getElementById("user-auth-input");
};

$.ajax({
    url: 'https://rsgeborgen.zportal.nl/api/v3/oauth/token',
    type: 'POST',
    dataType: 'json',
    contentType: 'text',
    data: body,
    complete: function(result) {
        //called when complete
        alert(result);
    },

    success: function(result) {
        //called when successful
        alert(result);
    },

    error: function(result) {
        //called when there is an error
        alert(result);
    },
});

0 个答案:

没有答案