简体   繁体   中英

send data using post without ajax

I want to send data in post method without using ajax, because I want to develop this application for blackberrry version5 and below. and blackberry does not support ajax.

I am sending data using post as:

  function handleLogin() {

    var form = $("#loginForm");
    var u = $("#username", form).val();
    var p = $("#password", form).val();
    var d = $("#dob", form).val();

    if (u != '' && p != '') {

        var finalStr = u + encodeURIComponent("|^") + p + encodeURIComponent("|^") + encodeURIComponent("|^") + "X" + encodeURIComponent("|^") + d + encodeURIComponent("|^") + "1.0" + encodeURIComponent("|^|$");
        var encodedURL = encodeURI(intranetUrl + "customer/Ri_logon5.asp?requestString=");
        var parameters = decodeURIComponent(finalStr);
        alert("param:" + parameters);
        post_to_url(intranetUrl + "customer/Ri_logon5.asp", finalStr);




    } else {

        alert("You must enter a username and password", function () {});
        $("#submitButton").removeAttr("disabled");
    }


}


function post_to_url(url, params) {
    var form = document.createElement('form');
    form.action = url;
    form.method = 'POST';

    alert("%%%%%%before" + params);
    var postParam = encodeURIComponent(params);
    postParam = decodeURIComponent(postParam);

    alert("%%%%%%" + postParam);
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'requestString=';
    input.value = params;
    document.body.appendChild(input);
    form.appendChild(input);
    form.submit();

}

Here in both alert of post_to_url() function I can read the parameters.As manish|^info1234|^|^X|^11111985|^1.0|^|$

But on server side they receive parameter as requestString%3D=manish%7C%5Einfo1234%7c%5E%7C%5EX%..... means special character are not receiving correctly on server side. But in alert they are showing correctly. Why is it so? Any help will be appreciated.

If I use ajax as ;

$.post(encodedURL, parameters ,
            function(data) {
              alert("Data Loaded: " + data);
            });

Then why thereis no need of decode string on server side?

You have to decode the URL on the server side. Like in Java you can use

String result = URLDecoder.decode(url, "UTF-8");

You encode url while sending data and decode in the server end. I think that is the best practice.

Using jQuery you can encode url using,

encodeURIComponent

CGI parameters are always given as key1=value1&key2=value2... where both keys and values may be URI encoded if they include any special characters.

When you do:

input.name = 'requestString=';
input.value = params;

you're producing a single key of requestString , and any special characters already in params has to be URI encoded. This will likely end up with your values being double encoded .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM