1

I am stuck with the problem at java script side

1)

var text='{"type": "comTocomp","data":[{ "1" : "Key Parameters" , "2" : "Steel - Sponge/ Pig Iron"},   
          { "1" : "No of Companies" , "2" : "6,6 %  "  }]}';

This is my var text i want to send it server side for generating the excel sheet and also the browser should prompt with popup as to save the file for this i am doing

2)

       var url="/XXX/YYYY/genrateSheet";       // URL OF CONTROLLER
       var encode=url+ "?data="+text;          // URL + PARAMETER+ TEXT CONTENT 
        var resUri=encodeURIComponent(encode); // TRIED TO ENCODE BUT DOESN'T WORK
        **window.location.replace(resUri);**       // CALLING TO CONTROLLER TO PROMPT POPUP

The problem is as in var text if it content some special character like % , . the browser shows

 The character encoding of the plain text document was not declared

But none of the special character then works fine for me.

I have Google a lot but want to encode the url with the use window.location.replace(resUri)

any help would be help me a lot.

Thanks in Advance

2
  • This has nothing to do with Java. Commented Dec 4, 2014 at 14:06
  • @fge yeh right by mistake.... :) Commented Dec 4, 2014 at 14:11

1 Answer 1

2

You need to encode the value of the querystring not the querystring itself:

var url="/XXX/YYYY/genrateSheet";       // URL OF CONTROLLER
var resUri = url + "?data=" + encodeURIComponent(text); // querystring
window.location.replace(resUri);

Saying that, text is quite long, so you should consider posting it to the new page, instead of passing it in the URL. One way to do that using jquery is to create and submit a hidden form containing the data you want:

$('button').on('click', function() {

    var text='{"type": "comTocomp","data":[{ "1" : "Key Parameters" , "2" : "Steel - Sponge/ Pig Iron"}, { "1" : "No of Companies" , "2" : "6,6 %  "  }]}';

    // create a hidden form    
    var theForm = $('<form action="/XXX/YYYY/genrateSheet" method="post"/>').hide();

    // create a textarea named data and set your json inside it
    var theTextarea = $('<textarea name="data"/>').val(text); // data in your case

    // add the textarea to the form
    theForm.append(theTextarea);

    // submit the form
    theForm.submit();
});

Working jsfiddle

Sign up to request clarification or add additional context in comments.

3 Comments

@Anurag I have updated my answer to show one way to post the data
actually my text data is very long in that case at server side it shows "Error parsing HTTP request header"... wat should i do now
with the use window.location.replace it is unsuccess full

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.