【问题标题】:jQuery AJAX POST FormjQuery AJAX POST 表单
【发布时间】:2019-08-03 10:10:25
【问题描述】:

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

<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

<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>

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

{
    "UserDetails": [
        {
            "status": "Enabled",
            "UserID": "ABCS884784",
            "UserNo": "897363762827"
        }
    ],
    "customerStatus": null,
    "responseCode": 0,
    "responseMessage": "Details Fetched Successfully "
}

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

"{\"customerStatus\":null,\"responseCode\":-2,\"responseMessage\":\"Inputs are invalid.\"}" 

这意味着body值没有被填满,到目前为止我的代码有什么问题

任何帮助将不胜感激,

【问题讨论】:

  • 请澄清$('0').val()是什么?
  • 这是频道的默认值

标签: javascript html ajax forms post


【解决方案1】:

删除dataType: 'text',因为 jQuery 可以智能地猜测响应中的预期内容(xml、json、脚本或 html)。同时删除processData: false,因为您没有发送 DOMDocument 或其他未处理的数据。

$('#my-form').submit(function () {
    var formData = {
         MSISDN : $('#MSISDN').val(),
         channel : $('#channel').val()
    };
    $.ajax({
        url: 'https://apiurl.com/users',
        type: 'post',
        headers: {
        'accept': 'application/json;charset=UTF8',
        'content-type': 'application/json',
        'api-key': 'judmkd895849438'
      },
        contentType: 'application/json',
        data: JSON.stringify(formData),
        success: function( data, textStatus, jQxhr ){
            console.log(data);
            $('#response pre').html( JSON.stringify( data ) );
        },
        error: function( jqXhr, textStatus, errorThrown ){
            console.log( errorThrown );
        }
    });
 });

【讨论】:

  • 您好,感谢您的回复,但控制台或#response 上仍然没有打印,如果我删除代码顶部的函数,则会打印但说错误空值
  • 可能端点只需要普通的 javascript 对象。你能替换data: { MSISDN : 'Test', channel : 'Test' }
  • 嗨@Hasta Tamang,替换了数据但仍然没有响应打印。
  • 仍然输入无效?
  • 响应没有显示任何错误但不会打印结果响应:(
【解决方案2】:

您在加载后立即调用 ajax,但您需要在提交时调用它试试这个。

  $('#my-form').submit(function () {
    $.ajax({
        url: 'https://apiurl.com/users',
        type: 'post',
        headers: {
        'accept': 'application/json;charset=UTF8',
        'content-type': 'application/json',
        'api-key': 'judmkd895849438'
      },
        contentType: 'application/json',
        data: { "MSISDN": $('#MSISDN').val(), "channel": $('#channel').val() },
        success: function( data, textStatus, jQxhr ){
            $('#response pre').html( JSON.stringify( data ) );
        },
        error: function( jqXhr, textStatus, errorThrown ){
            console.log( errorThrown );
        }
    });
 });

【讨论】:

  • 嗨@Josef Katič 感谢您的建议,但#response pre 没有打印任何内容,如何打印并显示在html 页面上?
  • @Bireon 尝试将脚本放在文件末尾。也可以试试console.log(data);
  • 控制台日志上仍然没有显示任何内容,我也希望在 html 页面上打印它
  • @Bireon 我已删除 JSON.stringify() 立即尝试。我已经更新了答案。
  • 还是不行,我也试了好几个都没有运气:(
猜你喜欢
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 2017-02-18
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多