jQuery AJAX post不起作用,但使用curl可以。

3

我正在尝试使用jQuery(用于BaseCamp API)通过ajax进行POST请求,但似乎无法使其正常工作。我可以使用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);
                }
            })

有人有任何想法吗?

谢谢!


1
当您在Firebug中查看它时,返回的错误是什么? 这可能是跨域问题吗? - seanmonstar
你是在同一个域名下发布吗? - karim79
1
只是提供信息,被接受的答案中的链接指向 Go-Daddy 停放的域名页面。 - Joshua Miller
没问题,@JoshuaMiller,我已经用archive.org上该页面的链接替换了它。 - Don Kirkby
2个回答

4

正如karim79所说,您不能发布到不同的域。

有关更多选项,请参见Nathan的文章(链接)


2
将其发布到您的服务器上,从应用程序代码将帖子传递给Basecamp,并将消息传回。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接