0

POST of XMLHttpRequest

Hi all,

I want use POST of XMLHttpRequest with tow parameters. So I did the follwing:

CODE
request = new XMLHttpRequest();
var url = 'http://localhost:8080/sendMessage.do?[b]userId[/b]=' + 'HoaTat' + '[b]&newStatus[/b]=' + '?!@#$%^&*';
request.open('POST', url, false);
request.timeout = 120;
request.send();

But I discovered it is only GET function because '?!@#$%^&*' characters can not send to server.
Next, I changed third parameter 'false' -> 'true'. However, it is error and can not go to server.
How to solve this problem or give me a simple example? http://developer.yahoo.net/forum/style_ima...icons/icon1.gif

Thanks and Regards,
Hoa Tat.

by
10 Replies
  • Try using ' encodeURIComponent("$#&**")' to encode the parameters

    Regards
    Jean-Noel
    0
  • I hope the below helps, but any values appended to the end of a url passed to the XMLHttpRequest() values are by default $_GET[] values.

    You would be better served sending the information via URL() in the postData rather than an XMLHttpRequest().

    How how do an XMLHttpRequest via the URL) object...
    CODE
    var url = new URL();
    var text = encode( "a lot of &&& bad text" );
    url.postData = "x=123&y=456&q=" + text;
    url.location = "http://www.example.com/myscript.php";
    //Async = false
    var contents = url.fetch();
    XMLdoc = XMLDOM.parse(content);
    //or
    //Async = true
    url.fetchAsync(url_done);

    //Function to call when done with Async
    function url_done( url )
    {
    print( "fetch complete" );
    XMLdoc = XMLDOM.parse(url.responseData);
    }


    Note: you still need to encode the values you are sending across the post.

    Hope this helps.
    0
  • Hi jngadreau and WidgetRealm,

    Thanks for your support and your method so much.

    All my love,
    Hoa Tat.
    0
  • QUOTE (WidgetRealm @ Jun 8 2009, 05:44 AM) <{POST_SNAPBACK}>
    I hope the below helps, but any values appended to the end of a url passed to the XMLHttpRequest() values are by default $_GET[] values.

    You would be better served sending the information via URL() in the postData rather than an XMLHttpRequest().

    How how do an XMLHttpRequest via the URL) object...
    CODE
    var url = new URL();
    var text = encode( "a lot of &&& bad text" );
    url.postData = "x=123&y=456&q=" + text;
    url.location = "http://www.example.com/myscript.php";
    //Async = false
    var contents = url.fetch();
    XMLdoc = XMLDOM.parse(content);
    //or
    //Async = true
    url.fetchAsync(url_done);

    //Function to call when done with Async
    function url_done( url )
    {
    print( "fetch complete" );
    XMLdoc = XMLDOM.parse(url.responseData);
    }


    Note: you still need to encode the values you are sending across the post.

    Hope this helps.


    For the record, this is how I would suggest doing it too. Thanks Brian!

    -Jeremy
    0
  • Quite welcome.
    0
  • QUOTE (WidgetRealm @ Jun 9 2009, 07:05 AM) <{POST_SNAPBACK}>
    Quite welcome.


    QUOTE (WidgetRealm @ Jun 9 2009, 07:05 AM) <{POST_SNAPBACK}>
    Quite welcome.


    Hello,

    I am trying to post data (text) to a server - and I have gone through the documentation and the example provided above. Here is my code snippet:

    var myURL = new URL("http://xxx.xxx.xxx.xxx:xxxx/");
    var text = encode( "Malformed &&&&& Text !!! *** 2222" );
    url.postData = text;

    If I run this in the emulator, I get the following error:
    WE 00:00:12:186: [T:30402] ReferenceError: encode is not defined (fullscreen.js: Line 80)


    Thanks for the help.
    0
  • That's because encode is not a Javascript function. See this page for functions you can use: http://xkr.us/articles/javascript/encode-compare/
    0
  • QUOTE (Gurminder @ Mar 29 2010, 09:40 AM) <{POST_SNAPBACK}>
    I am trying to post data (text) to a server - and I have gone through the documentation and the example provided above.

    Hi,
    The documentation is wrong, sorry about the confusion. Like has been already said, use encodeURIComponent instead.

    I'll make sure the docs are updated.

    - Ben
    0
  • QUOTE (Benjamin Toll @ Mar 29 2010, 11:58 AM) <{POST_SNAPBACK}>
    Hi,
    The documentation is wrong, sorry about the confusion. Like has been already said, use encodeURIComponent instead.

    I'll make sure the docs are updated.

    - Ben



    Okay makes sense...no more errors now... thanks!
    0
  • QUOTE (Benjamin Toll @ Mar 29 2010, 11:58 AM) <{POST_SNAPBACK}>
    Hi,
    The documentation is wrong, sorry about the confusion. Like has been already said, use encodeURIComponent instead.

    I'll make sure the docs are updated.

    - Ben


    re: documentation fix

    This is an example from the Desktop Widgets Konfabulator 4.5 reference manual for URL.postData:

    http://manual.widgets.yahoo.com/wwhelp/wwh...RL.html#1183350
    and
    http://widgets.yahoo.com/static/downloads/...ference_4.5.zip

    which has been removed from the latest TV Widgets documentation:

    http://developer.yahoo.com/connectedtv/devguide/

    Thank you,
    Kelly
    0

Recent Posts

in General - Yahoo! TV Widgets