jQuery AJAX post 不工作,但适用于curl
我正在尝试使用 jQuery(对于 BaseCamp API)通过 ajax 发出发布请求,但我似乎无法让它工作。我可以使用curl 让它正常工作,所以我知道这是我在使用jQuery 时做错的事情。这是有效的curl 命令:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -u my.user.name:my.password -d "<time-entry><person-id>123456</person-id><date>08/23/2009</date><hours>8</hours><description>This is a test.</description></time-entry>" https://my.url.updatelog.com/todo_items/1234567/time_entries.xml
这是我正在尝试使用jQuery 的代码:
var post_url = bc_base_url + "/todo_items/" + todo_id + "/time_entries.xml";
var xmlData = "<time-entry><person-id>" + bc_user_id + "</person-id>" +
"<date>" + date + "</date>" +
"<hours>" + time + "</hours>" +
"<description>" + description + "</description>" +
"</time-entry>";
$.ajax({
type: "POST",
url: post_url,
data: xmlData,
dataType: "xml",
contentType: "application/xml",
username: "my.user.name",
password: "my.password",
processData: false,
success: function(msg) {
alert("successfully posted! msg: " + msg + ", responseText = " + this.responseText);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("error : " + textStatus + ", errorThrown = " + errorThrown);
alert("this.reponseText = " + this.responseText);
}
})
有人有任何想法吗?
谢谢!
I'm trying to make a post request via ajax using jQuery (for the BaseCamp API), and I can't seem to get it to work. I can get it to work using curl just fine, so I know it's something I'm doing wrong with jQuery. Here's the curl command which works:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -u my.user.name:my.password -d "<time-entry><person-id>123456</person-id><date>08/23/2009</date><hours>8</hours><description>This is a test.</description></time-entry>" https://my.url.updatelog.com/todo_items/1234567/time_entries.xml
and here's the code I'm trying with jQuery:
var post_url = bc_base_url + "/todo_items/" + todo_id + "/time_entries.xml";
var xmlData = "<time-entry><person-id>" + bc_user_id + "</person-id>" +
"<date>" + date + "</date>" +
"<hours>" + time + "</hours>" +
"<description>" + description + "</description>" +
"</time-entry>";
$.ajax({
type: "POST",
url: post_url,
data: xmlData,
dataType: "xml",
contentType: "application/xml",
username: "my.user.name",
password: "my.password",
processData: false,
success: function(msg) {
alert("successfully posted! msg: " + msg + ", responseText = " + this.responseText);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("error : " + textStatus + ", errorThrown = " + errorThrown);
alert("this.reponseText = " + this.responseText);
}
})
Anyone have any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 karim79 所说,您不能发布到不同的域。
请参阅 Nathan 的帖子 了解更多选项。
As karim79 said, you can't post to a different domain.
See Nathan's post for more options.
将其发布到您的服务器,从应用程序代码将帖子传递到 Basecamp,然后将消息传回。
Post it to your server, pass the post onto basecamp from the application code, and pass the messages back down.