【问题标题】:My Jquery.ajax method is getting an error status in response despite the successful transfer of json尽管成功传输了 json,但我的 Jquery.ajax 方法仍收到错误状态作为响应
【发布时间】:2014-12-31 12:50:23
【问题描述】:

这是我的 jquery 方法:

<script>
    function onNotify() {
        console.log(jQuery('form #Name').val());
        var deviceInfo = platform;
        var for_name = jQuery('form #Name').val();
        console.log(for_name);
        var for_email = jQuery('form #Email').val();
        var for_phone = jQuery('form #phone').val();
        var for_checkindate = jQuery('form #date-picker-2').val();

        var for_checkoutdate = jQuery('form #date-picker-out').val();
        console.log(for_checkindate);
        console.log(for_checkoutdate);
        var appointment = {
            "inquirer": for_name,
            "email": for_email,
            "phone": for_phone,
            "checkoutDate": for_checkoutdate,
            "checkinDate": for_checkindate,

        };
        console.log(appointment);
        var hostname = document.URL.concat("bookings");
        console.log(hostname);
        jQuery.ajax({
            type: "POST",
            url: document.URL.concat("bookings"),
            crossDomain: true,
            data: appointment,
            dataType: "json",
            success: function (response) {
                setTimeout(90000);
                console.log("response with:", response);
            },
            error: function (error) {
                console.log("ERROR:", error);
            },
            complete: function () {
                jQuery('#Message-success-label').show();
                jQuery('#Message-success-label').fadeOut(8000);
                jQuery('form #Name').val('');
                jQuery('form #Email').val('');
                jQuery('form #phone').val('');
                jQuery('form #date-picker-2').val('');
                jQuery('form #date-picker-out').val('');
                console.log("done");
            }
        });
    }
</script>

这是调用方法的表单:

<form class="form-inline" role="form">

        <div class="slideform form-group">
            <label style="font-family:'Source Sans Pro';font-weight:400" for="Name">Name</label>
            <input type="name" class="form-control" id="Name" placeholder="Name" name="your-name" required="">
        </div>
        <div class="slideform form-group">
            <label style="font-family:'Source Sans Pro';font-weight:400" for="phone">Phone Number</label>
            <input type="number" class="form-control" id="phone" placeholder="Phone Number" name="your-phone" required="">
        </div>
        <br>
        <br>
        <div class="slideform form-group">
            <label style="font-family:'Source Sans Pro';font-weight:400" for="Email">Email</label>
            <input type="email" class="form-control" id="Email" placeholder="Email" name="your-email" required="">
        </div>
        <div class="slideform checkin form-group date-form form-horizontal">
            <div class="control-group">
                <label style="font-family:'Source Sans Pro';font-weight:400" for="date-picker-2" class="control-label">Check In</label>
                <div class="input-group controls">
                    <input id="date-picker-2" type="text" class="date-picker form-control" placeholder="Check In" name="your-checkindate" required="">
                    <label for="date-picker-2" class="input-group-addon btn"><span class="glyphicon glyphicon-calendar"></span>
                    </label>
                </div>
            </div>
        </div>
        <br>
        <br>
        <div class="slideform checkout form-group date-form form-horizontal">
            <div class="control-group">
                <label style="font-family:'Source Sans Pro';font-weight:400" for="date-picker-2" class="control-label">Check Out</label>
                <div class="input-group controls">
                    <input id="date-picker-out" type="text" class="date-picker form-control" placeholder="Check Out" name="your-checkoutdate" required="">
                    <label for="date-picker-out" class="input-group-addon btn"><span class="glyphicon glyphicon-calendar"></span>
                    </label>
                </div>
            </div>
        </div>


        <input type="submit" onclick="onNotify()" class="btn btn-default" value="Book a stay">
        </button>
        <p><span style="color:red;display:none" id="messagebook-success"><label id="Message-success-label" class="success-message">Thank you. We have received your request and shall revert shortly.</label></span>
        </p>
    </form>

虽然我的代码设法通过这种格式的 post 成功地将 json 发送到服务器:

{ inquirer: 'Jon',
  email: 'jogn@example.com',
  phone: '34659124',
  checkoutDate: '12/18/2014',
  checkinDate: '12/03/2014' }

我收到错误状态似乎是因为,我在浏览器控制台中收到以下错误:

ERROR: 
Object {readyState: 0, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}

同样在点击提交按钮时,当 url 更改为:

http://localhost:9001/?your-name=Jon&your-phone=34659124&your-email=jon%40example.com&your-checkindate=12%2F03%2F2014&your-checkoutdate=12%2F18%2F2014

尽管它是一个 ajax 调用,但整个页面会重新加载?我对这个问题束手无策,似乎没有任何意义,请指导..

【问题讨论】:

  • 您的 JSON 无效。见json.org
  • 你能否详细说明一下,以便我可以解决问题,然后研究更多关于 json 的内容。我实际上是研究 json 的新手。

标签: javascript jquery ajax json twitter-bootstrap


【解决方案1】:

在 JSON 中使用双引号。 另外,如果您不想实际提交表单,请钩住表单提交事件并使用 event.preventDefault() 而不是调用 onclick 属性中的函数。

$( "#myForm" ).submit( function(event) {
    event.preventDefault();
    onNotify();
});

或者更新你的函数:

function onNotify(event) {
  event.preventDefault();

  // your function body here
}

并使用:

$( "#myForm" ).submit( onNotify );

【讨论】:

  • 我使用双引号对吗? - var 约会 = { "inquirer": for_name, "email": for_email, "phone": for_phone, "checkoutDate": for_checkoutdate, "checkinDate": for_checkindate, };
  • 只需将您的约会设为数组并调用 var jsonString = JSON.stringify(appointment);
猜你喜欢
  • 1970-01-01
  • 2020-10-25
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 2022-01-22
  • 2022-12-31
相关资源
最近更新 更多