7*4 2019-03-13 07:07 采纳率: 50%
浏览 34

JSON响应打印

I have a ajax request script with post method using body value, what I want is, to print the response value.

 $('#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: { "firstname": $('#firstname').val(), "lastname": $('#lastname').val() },
        success: function( data, textStatus, jQxhr ){
            $('#response pre').html( JSON.stringify( data ) );
        },
        error: function( jqXhr, textStatus, errorThrown ){
            console.log( errorThrown );
        }
    });
 });

and the form script is:

<form id='my-form'>
    <div><input type='text' name='firstname' id='firstname' placeholder='Firstname' /></div>
    <div><input type='text' name='lastname' id='lastname' placeholder='Lastname' /></div>
    <div><input type='submit' value='Submit' /></div>
</form>

<!-- where the response will be displayed -->
    <div id="response">
    <pre></pre>
</div>

but nothing display on screen, anyone know why?

thanks

  • 写回答

1条回答 默认 最新

  • weixin_33697898 2019-03-13 08:02
    关注

    As Roy stated, you'll need to prevent the browser from submitting the form. You'll have to prevent the default event from firing as you're handling it via an AJAX request.

    $('#my-form').submit(function (event) {
        event.preventDefault();
        $.ajax({
            url: 'https://apiurl.com/users',
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line