代码之家  ›  专栏  ›  技术社区  ›  aalaap

如何在AdobeAir应用程序中通过javascript将XML数据发布到URL?

  •  1
  • aalaap  · 技术社区  · 15 年前

    我正在编写一个应用程序,它从一个URL下载一个XML字符串,并 POST 发送到另一个URL(设置为处理传入的URL) XML “字段”。我已经把第一部分做好了——它下载了XML,我可以 alert() 但我不知道该怎么做 将数据发送到服务器。

    function pull() {
        var myLoader = new air.URLLoader();
        var myRequest = new air.URLRequest('http://something/something.xml');
        myLoader.addEventListener(air.Event.COMPLETE, pulled);
        myLoader.load(myRequest);
    }
    
    function pulled(evt) {
        if (evt.target.bytesTotal>0) {
            // alerting shows the full string just fine
            alert(evt.target.data);
    
            var myLoader = new air.URLLoader();
            var myRequest = new air.URLRequest('http://someplace/push.php');
            myRequest.method = air.URLRequestMethod.POST;
            // myVars = new air.URLVariables("xml="+evt.target.data); // 
            // alert(evt.target.data.toUpperCase());
            myRequest.data = "xml="+evt.target.data; // myVars;
            myLoader.dataFormat = air.URLLoaderDataFormat.TEXT;
            myLoader.addEventListener(air.Event.COMPLETE, pushed);
            myLoader.load(myRequest);
        }
    }
    

    我做了第二个服务器php echo 的内容 xml 变量,但我无法获取XML字符串的确切内容。有件事我正在处理 myRequest.data 和/或 dataFormat 比特。

    有人能解决这个问题吗?我知道这可能是一件简单的事情,但我现在已经无计可施了。

    这是我的第一个空中应用程序。

    另一个相关问题(或子问题)是…

    alert(evt.target.data);               // shows an alert box with the XML
    alert(typeof evt.target.data);        // shows String
    alert(evt.target.data.toUpperCase()); // shows the xml converted to upper case
    alert(encodeURI(evt.target.data));    // shows up blank.
    alert(escape(evt.target.data));       // shows up blank.
    

    为什么??

    1 回复  |  直到 15 年前
        1
  •  1
  •   Gabriel    15 年前

    错误似乎是您将参数分配给“数据”的方式…使用urlvariables。

    var params:URLVariables = new URLVariables();
    params.[name of parameter] = [value];
    

    ---就像params.xml=(您的xml)…从你的例子来看:

    // uses the dynamic object to add the 'xml' property to 'params' at runtime.
    params.xml = evt.target.data
    

    然后将request.data更改为request.data=params;

    --Urlvariables是动态的-所以您可以像上面描述的那样添加属性。

    对于一个基本的例子-更完整的是,我在这里所拥有的: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html