我无法将JSON发布到服务器,因为AJAX以某种方式放弃了内容类型。[使用此库] [1]以及Jquery和Bootstrap我发现了问题:
这是AJAX代码:
var items;
$('#orderform').on('submit',function () {
console.log(items);
$.ajax({
url: "/order",
type: "POST",
data: JSON.stringify({"order": '555'}),
contentType: "application/json; charset=utf-8",
dataType: "text/html; charset=utf-8",
success: function(result) {
var data = "Check";
}
});
编辑:这是内容类型: application / x-www-form-urlencoded
编辑2:使用网页的绝对路径作为URL我发现它在回发到服务器时收到405没有分配内容标题。
编辑3:这是JSFiddle:
var order;
var adjustment;
var group = $("ol.simple_with_animation").sortable({
group: 'simple_with_animation',
pullPlaceholder: false,
// animation on drop
onDrop: function($item, container, _super) {
var $clonedItem = $('<li/>').css({
height: 0
});
$item.before($clonedItem);
$clonedItem.animate({
'height': $item.height()
});
$item.animate($clonedItem.position(), function() {
$clonedItem.detach();
_super($item, container);
});
var data = group.sortable("serialize").get();
order = data[1];
console.log(order);
},
// set $item relative to cursor position
onDragStart: function($item, container, _super) {
var offset = $item.offset(),
pointer = container.rootGroup.pointer;
adjustment = {
left: pointer.left - offset.left,
top: pointer.top - offset.top
};
_super($item, container);
},
onDrag: function($item, position) {
$item.css({
left: position.left - adjustment.left,
top: position.top - adjustment.top
});
},
serialize: function(parent, children, isContainer) {
return isContainer ? children.join() : parent.text();
},
tolerance: 6,
distance: 10
});
$(document).ready(function() {
$(':button').on('click', function() {
console.log(order);
$.ajax({
url: "http://ec2-54-244-61-189.us-west-2.compute.amazonaws.com/order",
type: "POST",
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"info": order
}),
async: false,
success: function(result) {
window.location.href = result.redirect;
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}); //END mybutton click
}); //END document.read
body.dragging,
body.dragging * {
cursor: move !important;
}
.dragged {
position: absolute;
opacity: 0.5;
z-index: 2000;
}
ol.simple_with_animation li.placeholder {
position: relative;
/** More li styles **/
}
ol.simple_with_animation li.placeholder:before {
position: absolute;
/** Define arrowhead **/
}
#GamblerOrderSource,
#GamblerOrderTarget {
min-height: 50px;
margin: 0px 25px 10px 0px;
padding: 2px;
border-width: 1px;
border-style: solid;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
list-style-type: none;
list-style-position: inside;
}
#GamblerOrderSource {
border: 2px dashed #f8e0b1 !important;
background-color: #fefcf5 !important;
}
#GamblerOrderTarget {
border: 2px dashed #add38d !important;
background-color: #f6fbf4 !important;
}
#GamblerOrderSource li {
background-color: #fcf8e3;
border: 1px solid #fbeed5;
color: #c09853;
}
#GamblerOrderTarget li {
background-color: #ebf5e6;
border: 1px solid #d6e9c6;
color: #468847;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sortable/0.9.13/jquery-sortable-min.js"></script>
<form id="orderform" method='POST'>
<div id='GamblerOrderSource'>
<ol class='simple_with_animation'>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
</div>
<div id='GamblerOrderTarget'>
<ol class='simple_with_animation'>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
</div>
<input type="submit" value="Continue" />
</form>
文件
答案 0 :(得分:2)
使用dataType: 'json'
发送json
数据。您没有在服务器上发送json数据。您需要更改dataType
以发送json
数据
尝试使用这个ajax代码,如果它已经是正确的对象,则无需对json对象进行字符串化。我已经在我这边测试了它,希望它能帮到你。
$.ajax({
type:"POST",
url: "YOUR PATH TO FILE OR METHOD",
data:{
order:'555'
},
dataType: 'json',
success: function(data){
if(data['success']=='true'){
echo 'success';
}else{
if(data['success']=='false'){
echo 'not success';
}
}
},
error: function(xhr, status, error) {
console.log(status);
console.log(error);
}
});
答案 1 :(得分:1)
只需将您的dataType:“text / html; charset = utf-8”更改为dataType:json