weixin_33704591 2018-02-13 15:20 采纳率: 0%
浏览 30

使用GET的AJAX API

I am working on a block of code that will fire off an API if a checkbox is checked. I've tested the API and it works. I had the form functioning before turning it into a widget. (at least I think I did??)

So here is the script that won't GET form values when checked:

<script type="text/javascript">
(jQuery || $)(function($) {
$('#survessay-form').on('submit', function() {
    // the jquery element of the form
    var $this = $(this);

    if($this.find('input[type="checkbox"]').is('checked')) {
        $.ajax({
            type: "GET",
            url: `https://www.example.com/?cmd=api-co-reg&apik=ukp!BjItWgMi6Qk&Email=${$('#awf_field-95505011').val()}&FirstName=${$('#awf_field-95505010').val()}&aff_sid=emailcoreg&cmp=1320&cxid=242570`
        }).done(function(data) {
            data = data.result;
            $("#checkedmessage").html("<p>Thanks, " + data.name + ", you're now a Swagbucks participant!</p>");
            // continue to the confirmation page
        }).fail(function(data) {
            // verify data
            data = data.responseJSON;
            $("#errormessage").html("Error: Please check the format of your input");
        });
    }

    // stops probagation of event (doesn't submit the request to the php page)
    return false;
 });
});

So essential this should submit the API when the checkbox is checked and the user clicks the submit button but it continues on to the forms action method. Without submitting the request. What am I missing here?

  • 写回答

1条回答 默认 最新

  • weixin_33691700 2018-02-13 16:06
    关注

    Use type: GET if the version is earlier than 1.9.0. Else use method: GET. More also, your URL suppose to start and end with " instead of the single quote since some of your parameter's value has the single quote. Below is the edited version of your code. Try to use your browser developer tools to debug your code.

     (function($) {
    $('#survessay-form').on('submit', function() {
        // the jquery element of the form
        var $this = $(this);
    
        if($this.find('input[type="checkbox"]').is('checked')) {
            $.ajax({
                method: "GET",
                url: "https://www.example.com/?cmd=api-co-reg&apik=ukp!BjItWgMi6Qk&Email=" + $('#awf_field-95505011').val() + "&FirstName=" + $('#awf_field-95505010').val() + "&aff_sid=emailcoreg&cmp=1320&cxid=242570",
    success: function(data) {
                data = data.result;
                $("#checkedmessage").html("<p>Thanks, " + data.name + ", you're now a Swagbucks participant!</p>");
                // continue to the confirmation page
            }
          }).fail(function(data) {
                // verify data
                data = data.responseJSON;
                $("#errormessage").html("Error: Please check the format of your input");
        }
    
        // stops probagation of event (doesn't submit the request to the php page)
        return false;
     });
    })(jQuery);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行