【发布时间】:2015-12-06 08:04:15
【问题描述】:
您好,我创建了一个从表单获取数据的 JSON 对象,现在我想将其发布到 redmine API。这就是我到目前为止所做的。
<script>
// This is the creation of JSON object
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return {"project":o};
};
// This is the API linking and POSTING
$(document).ready(function(){
$("#submit").on('click', function(){
// send ajax
$.ajax({
url: 'http://localhost/redmine/projects.json', // url where to submit the request
type : "post", // type of action POST || GET
dataType : 'jsonp',
headers: { 'X-Redmine-API-Key': 'admin' },
data : JSON.stringify($('form').serializeObject()), // post data || get data
success : function(result) {
// you can see the result from the console
// tab of the developer tools
alert("Sucess");
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
</script>
<form action="" method="post">
First Name:<input type="text" name="name" maxlength="12" size="12"/> <br/>
Last Name:<input type="text" name="identifier" maxlength="36" size="12"/> <br/>
<!-- number:<input type="number" name="number" maxlength="36" size="12"/> <br/> -->
<textarea wrap="physical" cols="20" name="description" rows="5">Enter your favorite quote!</textarea><br/>
<p><input type="submit" /></p>
</form>
该帖子不起作用。 JSON 对象创建得很好,将其传递给 API 是个问题。我认为问题就在这里,
data : JSON.stringify($('form').serializeObject()),
如何将上面创建的 JSON 对象传递给数据。谢谢
【问题讨论】:
标签: javascript jquery json ajax api