【发布时间】: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