为 ajax post 创建 jquery 数组
create jquery array for ajax post
如何将 drp.getDateRange
的输出转换为数组,以便我可以通过 AJAX post 它?
我已更新此代码以表示下面给出的建议
<script>
var drp;
var myArray = [];
function makedatepicker() {
drp = $("#myDate").datepicker({});
}
function getRange() {
var data = $("#myOutput").serialize();
console.log("This is the serialized element");
console.dir(data);
$.ajax({
url: "myurl.com",
type: 'post',
data: data,
success: function(response) {
console.log("This is the response from your ajax script");
console.dir(response);
}
});
}
$(document).ready(function () {
makedatepicker();
});
</script>
更新
关于 JSON 的注释。默认编码将是 url 形式编码。如果您希望请求以 JSON 形式发送数据,则需要添加..
content-type: "application/json; charset=utf-8",
另外,如果您返回 JSON,您应该添加 ...
datatype : "json",
不确定您在后端使用哪种脚本语言,但如果 PHP,您可以像这样发回数组数据
echo json_encode($myArray);
我会将 JSON 内容添加到下面的示例代码中。
结束更新
如果您使用 .serialize(),您将其作为 ajax 数据发送,它将显示在您的 post 或获取数组中。
如果您正在使用数组,您可能需要使用 .serializeArray()
您可以使用 console.dir(someObject);
在您的开发者工具中查看对象或数组(Chrome 或 FF 中的 F12)
var data = $("#myOutput").serialize();
console.log("This is the serialized element");
console.dir(data);
$.ajax({
url: "myurl.com",
type: 'post',
datatype : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
beforeSend : function (){
console.log("Before Send: Data looks like..");
console.dir(data);
},
success: function(response) {
console.log("This is the response from your ajax script");
console.dir(response);
console.log("parsing JSON ...");
console.dir($.parseJSON(response));
}
});
Chrome 开发人员工具控制台,在这里您可以看到您 console.log 或 console.dir
的任何内容
您可以通过单击“网络”选项卡检查正在发送的 JSON。然后单击 ajax 脚本的名称,它会显示正在发送的数据。 (此外,单击 "response" 选项卡将显示您的脚本发回的内容。)
在下面的示例中,我使用上面的代码发送了一个 ajax 请求,它显示数据确实是一个 JSON 对象。
如何将 drp.getDateRange
的输出转换为数组,以便我可以通过 AJAX post 它?
我已更新此代码以表示下面给出的建议
<script>
var drp;
var myArray = [];
function makedatepicker() {
drp = $("#myDate").datepicker({});
}
function getRange() {
var data = $("#myOutput").serialize();
console.log("This is the serialized element");
console.dir(data);
$.ajax({
url: "myurl.com",
type: 'post',
data: data,
success: function(response) {
console.log("This is the response from your ajax script");
console.dir(response);
}
});
}
$(document).ready(function () {
makedatepicker();
});
</script>
更新
关于 JSON 的注释。默认编码将是 url 形式编码。如果您希望请求以 JSON 形式发送数据,则需要添加..
content-type: "application/json; charset=utf-8",
另外,如果您返回 JSON,您应该添加 ...
datatype : "json",
不确定您在后端使用哪种脚本语言,但如果 PHP,您可以像这样发回数组数据
echo json_encode($myArray);
我会将 JSON 内容添加到下面的示例代码中。
结束更新
如果您使用 .serialize(),您将其作为 ajax 数据发送,它将显示在您的 post 或获取数组中。
如果您正在使用数组,您可能需要使用 .serializeArray()
您可以使用 console.dir(someObject);
在您的开发者工具中查看对象或数组(Chrome 或 FF 中的 F12)var data = $("#myOutput").serialize();
console.log("This is the serialized element");
console.dir(data);
$.ajax({
url: "myurl.com",
type: 'post',
datatype : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
beforeSend : function (){
console.log("Before Send: Data looks like..");
console.dir(data);
},
success: function(response) {
console.log("This is the response from your ajax script");
console.dir(response);
console.log("parsing JSON ...");
console.dir($.parseJSON(response));
}
});
Chrome 开发人员工具控制台,在这里您可以看到您 console.log 或 console.dir
的任何内容您可以通过单击“网络”选项卡检查正在发送的 JSON。然后单击 ajax 脚本的名称,它会显示正在发送的数据。 (此外,单击 "response" 选项卡将显示您的脚本发回的内容。)
在下面的示例中,我使用上面的代码发送了一个 ajax 请求,它显示数据确实是一个 JSON 对象。