gpt4 book ai didi

jquery - 内容类型无法通过 AJAX Post 发送到服务器

转载 作者:行者123 更新时间:2023-12-01 01:21:48 26 4
gpt4 key购买 nike

我在将 JSON 发布到服务器时遇到问题,因为 AJAX 不知何故放弃了内容类型。[使用此库][1] 以及 Jquery 和 Bootstrap 我发现了问题:

  • Curl 适用于帖子,并在内容为 JSON 时在 Flask 应用中检索成功。
  • 经过多种方法和途径后,post 可以进入应用程序,但 ContentType 无法与 JSON 一起传输
  • 尝试了多种方法来格式化 JSON,但内容类型均无法识别。使用

这是 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";
}
});

编辑:这是内容类型:应用程序/x-www-form-urlencoded

编辑2:使用网页的绝对路径作为URL,我发现它在回发到服务器时收到405,未分配内容 header 。

编辑 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>

最佳答案

使用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);
}
});

关于jquery - 内容类型无法通过 AJAX Post 发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com