簡體   English   中英

盡管成功傳輸了json,我的Jquery.ajax方法仍在響應中顯示錯誤狀態

[英]My Jquery.ajax method is getting an error status in response despite the successful transfer of json

這是我的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更改為時,顯然會發送一個get請求:

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中使用雙引號。 另外,如果您不希望實際提交表單,請掛鈎表單提交事件並使用event.preventDefault(),而不要在onclick屬性中調用該函數。

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

或更新您的功能:

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

  // your function body here
}

並使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM