3

When I use dojo.xhrGet , I use it this way to send more than one parameter via the GET

dojo.xhrGet
({
    url:"MyServerPageURL?Param_A="+"ValueA"+"&Param_2="+"Value2",
    load: function(data)
    {
        //do something
    },
    preventCache: true,
    sync: true, 
    error: function(err)
    {
        alert("error="+err); 
    }
});

How could I do similar thing (send more than one parameter) when I have to use the dojo.xhrPost instead?

Bikash Gyawali
  • 969
  • 2
  • 15
  • 33

3 Answers3

6

You do not want to use postData parameter unless you want to send a raw POST string. You normally want to use the 'content' parameter. For example:

dojo.xhrPost({
    url: 'http://whatever...',
    contents: {
        ParamA: 'valueA',
        ParamB: 'valueB'
    },
    load: function(response) {
        // ...
    }
});

Note: Use 'contents' works for xhrGet also, eliminating the need to build up the query string yourself and append to the URL.

jhonny
  • 15
  • 4
ewh
  • 1,004
  • 9
  • 19
4

Try to use postData parameter. E.g:

        var myParameters= {"Param_A":"Value_A", "Param_B":"Value_B"};

        var xhrArgs = {
                        url: "postIt",
                        postData: dojo.toJson(myParameters),
                        handleAs: "text",
                        headers: { "Content-Type": "application/json", "Accept": "application/json" },
                        load: function(data) {

                        },
                        error: function(error) {

                        }
                    }


 var deferred = dojo.xhrPost(xhrArgs);
Andrei
  • 4,237
  • 3
  • 25
  • 31
  • On the server, I am getting null for request.getParameter. And why is the dojo.toJson required? Could you explain a bit, please? – Bikash Gyawali Apr 21 '11 at 17:40
  • postData - it is data that you wish to send as the request body.dojo.xhrPost do not do any processing of this It is merely passed through as the POST body. dojo.toJson required in this case because postData parameter should be string only. – Andrei Apr 21 '11 at 18:40
  • it's also a good idea to set the Accept value and Content Type to application/json var xhrArgs = {..., headers: { "Content-Type": "application/json", "Accept" : "application/json"}, ... } – Andrei Apr 21 '11 at 18:47
  • ok. under `doPost` method of servlet, when i call `request.getParameter("Param_A")`, i get null. i don't understand whats the problem. – Bikash Gyawali Apr 21 '11 at 19:51
  • What language do you use on the server-side? – Andrei Apr 21 '11 at 19:58
  • You should not use getParameter method. You need to get request body. Look this: http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data and this:http://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest – Andrei Apr 21 '11 at 20:34
  • Thanks. I used Java on server side and used the reply by Kdeveloper in http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data for server side. On client side, I used your suggestion. – Bikash Gyawali Apr 22 '11 at 08:54
  • Do not use postData unless you want to send raw post data to the servlet. This is the reason that request.getParameter() is not working as expected on the servlet side. Use the 'content' parameter to xhrPost as I mention in my answer. – ewh Apr 22 '11 at 19:00
  • In all cases "content" object would be passed as request body, not as query string. I did not known java, but i think method getParameter() would not working. – Andrei Apr 22 '11 at 19:19
0

For xhrPOst, it's possible to mention the form name to be posted. thus all your form elements get posted. If you want to pass some additional parameter then use hidden variable in the form that is posted.

Baz
  • 36,440
  • 11
  • 68
  • 94
Praveen
  • 41
  • 1
  • 6