首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery AJAX POST表单

jQuery AJAX POST表单
EN

Stack Overflow用户
提问于 2019-03-13 04:10:34
回答 2查看 111关注 0票数 1

我尝试将表单值解析为正文请求的数据变量,但正文总是返回空。这意味着我的脚本无法将用户的字段值类型打印到正文数据中。在HTML表单中:

代码语言:javascript
复制
<form id="my-form">
    <input type="text" id="MSISDN" name="MSISDN" placeholder="MSISDN"/>
    <input type="text" id="channel" name="channel" placeholder="channel"/>

    <button type="submit">Submit</button>
</form>

下面是我的javascript

代码语言:javascript
复制
<script>
$.ajax({
    url: 'https://apiurl.com/users',
    dataType: 'text',
    type: 'post',
    headers: {
    'accept': 'application/json;charset=UTF8',
    'content-type': 'application/json',
    'api-key': 'judmkd895849438'
  },
    contentType: 'application/json',


    data: JSON.stringify( { "MSISDN": $('#MSISDN').val(), "channel": $('0').val() } ),
    //data: JSON.stringify( {MSISDN: document.getElementById("MSISDN").value, channel: "0"} ),


    processData: false,
    success: function( data, textStatus, jQxhr ){
        $('#response pre').html( JSON.stringify( data ) );
    },
    error: function( jqXhr, textStatus, errorThrown ){
        console.log( errorThrown );
    }
});
</script>
<div id="response">
    <pre></pre>
</div>

如果请求成功,则响应为:

代码语言:javascript
复制
{
    "UserDetails": [
        {
            "status": "Enabled",
            "UserID": "ABCS884784",
            "UserNo": "897363762827"
        }
    ],
    "customerStatus": null,
    "responseCode": 0,
    "responseMessage": "Details Fetched Successfully "
}

但是在我的代码中,响应总是返回错误

代码语言:javascript
复制
"{\"customerStatus\":null,\"responseCode\":-2,\"responseMessage\":\"Inputs are invalid.\"}" 

这意味着主体值没有填满,我不知道到目前为止我的代码出了什么问题

任何帮助都将不胜感激,

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55129925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档