简体   繁体   中英

send request from jsp to servlet using gson

I am developing a web application using JSP , Servlets . I am using gson for sending request from jqGrid to Servlet .

Following is my code to send request to Servlet:

JSP

$.post('MyServletName?action=Activate&var11='+var11,
    function(responseJson) 
    {
        $.each(responseJson, function(index, item) 
        {
            alert(item);
        });
    });

Servlet Code

String str= request.getParameter("var11");

I am able to send the request to Servlet using above code. But if var11 contains % , then it is not sending the % sign and all characters after % to the Servlet .

So Please let me know what might be the problem?

The % (and other special characters in URLs) needs to be encoded. If you use the (optional) data parameter of the jQuery.post() function this will be handled for you. So change it to:

$.post('MyServletName?action=Activate', {var11 : var11}, function (responseJson) {
    $.each(responseJson, function (index, item) {
        alert(item);
    });
});

You could alternatively use the encodeURIComponent function, like so:

$.post('MyServletName?action=Activate&var11=' + encodeURIComponent(var11), function (responseJson) {
    $.each(responseJson, function (index, item) {
        alert(item);
    });
});

如果选择第一种方法,请不要忘记使用var11属性{"var11":var11}而不是{var11:var11}的引号。

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