如何从 JavaScript 发送带有数据作为 xml 元素的放置请求? [英] how to send put request with data as an xml element, from JavaScript?

查看:54
本文介绍了如何从 JavaScript 发送带有数据作为 xml 元素的放置请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据是一个 xml 元素 &我想用 JavaScript 发送 PUT 请求.我该怎么做?

My data is an xml element & I want send PUT request with JavaScript. How do I do this ?

参考:更新单元格

按照弗雷德里克的建议,我这样做了:

EDIT : As per fredrik suggested, I did this :

<form id="submitForm" method="PUT" enctype="application/atom+xml" action="https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1">
            <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
                <id>https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1</id>
                <link rel='edit' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1'/>
                <gs:cell row='2' col='1' inputValue='300'/>
            </entry>
            <input type="submit" value="submit"/>
        </form>

但是,它不会回写,而是肯定会返回 xml 文件,例如:

However, it doesn't write back but positively it returns xml file like :

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006' xmlns:batch='http://schemas.google.com/gdata/batch'>
    <id>https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1</id>
    <updated>2011-01-11T07:35:09.767Z</updated>
    <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#cell'/>
    <title type='text'>A2</title>
    <content type='text'></content>
    <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1'/>
    <link rel='edit' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/cells/0Aq69FHX3TV4ndDBDVFFETUFhamc5S25rdkNoRkd4WXc/od6/private/full/R2C1/1ekg'/>
    <gs:cell row='2' col='1' inputValue=''></gs:cell>
</entry>

这里,输入值为空!但是,它在我的 xml 字符串中是 300.对于相同的任何进一步的解决方案?

Here, inputvalue is empty ! However, it is 300 in my xml string. Any further solution for the same ?

推荐答案

由于 HTTP 协议只支持发送字符串,我不确定您是否能够做到这一点.但是您可以尝试使用 jQuery 的 ajax 方法 并将方法更改为 PUT 和内容类型以及发送序列化的 XML.

Since the HTTP protocol only supports sending string I'm un sure if you are able to do this. But what you could try is using jQuery's ajax method and changing the method to PUT and content type and send a serialized XML.

jQuery 文档说:

The jQuery documentation says:

发出的请求类型(POST"或GET"),默认为GET".注意:这里也可以使用其他HTTP请求方式,如PUT、DELETE等,但并非所有浏览器都支持.

The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

调用 ajax 调用:

Invoke a ajax call:

 $.ajax({
  url: 'ajax/test.html',
  type: 'PUT',
  contentType: 'text/xml',
  processData: false,
  data: xmlDocument,
  success: function(data) {
    console.log(data);
  }
});

希望它有效.

编辑:请提供有关您正在尝试执行的操作的更多信息/代码.

EDIT: Please provide some more information/code on what you are trying to do.

这篇关于如何从 JavaScript 发送带有数据作为 xml 元素的放置请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆