【问题标题】:how to send ajax put request from jQuery如何从 jQuery 发送 ajax put 请求
【发布时间】:2011-06-13 17:19:23
【问题描述】:

我想在授权用户后写回我的谷歌电子表格。授权完成。但是,要写回电子表格,我已发送 PUT 请求,如 here 所述。它来自 iGoogle 小工具。

我的 XML 元素是:

var cellUrl = "https://spreadsheets.google.com/feeds/cells/" + key + "/od6/private/full/R2C2";
var XMLData = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>" + 
                "<id>" + cellUrl + "</id><link rel='edit' type='application/atom+xml' href='" + cellUrl + "'/>" + 
                "<gs:cell row='2' col='2' inputValue='300'/>" + 
            "</entry>";

我发送 AJAX 请求为:

$.ajax({
                url: cellUrl,
                type: "PUT",
                contentType: 'application/atom+xml',
                processData: false,
                data: XMLData,
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert(errorThrown);
                }, success: function(data, textStatus, XMLHttpRequest){
                    alert("Succeeded");
                }
            });

不过,它没有回信,也没有显示任何警报!什么问题?

我应该使用 POST 来回信吗?我该怎么做?

为了安全起见,我隐藏了 cellURL 密钥。它类似于我的电子表格网址中的键。

【问题讨论】:

  • 闻起来像 StackOverflow.com 的问题...
  • 您能检查一下请求的实际提交位置吗?您可以为此使用 firebug/开发者工具。
  • 您使用的是哪个浏览器?根据 jquery 文档,并非所有浏览器都支持 PUTDELETE 请求类型。你能检查一下POST 会在哪里解决你的问题吗?

标签: jquery xml ajax put


【解决方案1】:

我的建议

  • 检查 'key' resourceID 是否应该转义
  • 如果仍然无法正常工作,请尝试将所有内容放入并添加和使用点击事件。对于 ajax,请使用 iframe。
  • 试试 $.ajax({... , data:$(XMLData), ...});

【讨论】:

    【解决方案2】:

    你能不能试试这样看看是否有错误:

    $.ajax({
            url: cellUrl,
            type: "PUT",
            contentType: 'application/atom+xml',
            processData: false,
            data: XMLData
        }, error: function(XMLHttpRequest, textStatus, errorThrown){
           alert(errorThrown);
        }, success: function(data, textStatus, XMLHttpRequest){
           alert("Succeeded");
        }
    
    );
    

    【讨论】:

    • 我已经添加了 dataType : "xml" 和你的代码,但它什么也没显示 :( !那里没有警报!
    • 您确定 $.ajax 请求正在执行吗?你能发出警报吗(“工作中!!”);就在ajax请求和确认之前?你是用 FF/Firebug 还是 Chrome 来调试它?
    • 好的!我也删除了那个错误。现在它显示“成功”。但是,它还没有写回数据!
    • 试试 alert(data + " , " + textStatus " , " + XMLHttpRequest);在成功功能中查看是否可以让您深入了解它为什么不起作用...
    猜你喜欢
    • 2011-01-10
    • 2016-01-06
    • 2015-07-10
    • 2022-12-25
    • 1970-01-01
    • 2017-01-14
    • 2018-01-10
    • 2016-07-25
    相关资源
    最近更新 更多