4

I have a data extracted from a textarea which is something like this:

TO WHOM IT MAY CONCERN:
                            This is to certify that Mr/Ms JOHN SMITH is enrolled in this institution for this First Semester of School Year 2013-2014
                            as a second year Psychology student. Below is his/her STATEMENT OF ACCOUNT.

What I've tried?

var letter = $('#textarea_letter').val();
var myJSON = {"letter": letter }; 
var strJSON = JSON.stringify(myJSON); 

$('#toPDF_button').attr('href', 'generate_pdf/'+ myJSON +'/pdf'); 
// this button then sends the letter to a function w/c generates the PDF

What did I get?

An Error_404 page

I need to pass the letter in the param url, this is how it looks like in the url:

localhost/accounting/generate_pdf/{"letter":"TO WHOM IT MAY CONCERN:\n\t\t\t\t\t\t\tThis is to certify that Mr/Ms JOHN S> SMITH is enrolled in this institution for this First Semester of School Year 2013-2014\n\t\t\t\t\t\t\tas a second year BSIT student. Below is his/her STATEMENT OF ACCOUNT."}/pdf

How can I be able to do this? I'm new to JSON. Thanks

UPDATE

After doing this var myJSON = encodeURIComponent(JSON.stringify(myJSON1));
here is the new URL but still gets the same error

http://localhost/accounting/accounting/generate_sta/%7B%22letter%22%3A%22TO%20WHOM%20IT%20MAY%20CONCERN%3A%5Cn%5Ct%5Ct%5Ct%5Ct%5Ct%5Ct%5CtThis%20is%20to%20certify%20that%20Mr%2FMs%20JOHN%20SMITH%20G.%20SEBUCAO%20is%20enrolled%20in%20this%20institution%20for%20this%20First%20Semester%20of%20School%20Year%202013-2014%5Cn%5Ct%5Ct%5Ct%5Ct%5Ct%5Ct%5Ctas%20a%20second%20year%20Psychology%20student.%20Below%20is%20his%2Fher%20STATEMENT%20OF%20ACCOUNT.%22%7D/pdf
6
  • Instead of putting it as part of the URL, you should probably submit the form. Commented Sep 20, 2013 at 8:53
  • The 404 error is not related to JSON. It is related to the absence of the file requested. Since you tagged this post as Codeigniter, it means either you do not have a controller file AccountingController{} or your controller class is there, but the method generate_pdf() is missing. I voted you down for not providing all the information and keeping me guessing. Try and make it easy for people to help you. Commented Sep 20, 2013 at 8:58
  • I'm sorry if my question is a bit vague, but I made sure already that I have the function for generate_pdf(). I have a hunch that its something to do with the URL encoding stuff. Commented Sep 20, 2013 at 9:07
  • Its really with the json part, I tried to substitute a dummy param and it worked. So the Problem really is in the JSON part Commented Sep 20, 2013 at 9:21
  • 1
    in your URL I don't see index.php so did you remove it with htaccess. and if yes, then why do you have accounting/accounting. what I'm trying to say is: are you sure that this url is well formed? because the last part of your url is considered as a segment which will not produce a 404 error. Commented Sep 21, 2013 at 7:30

5 Answers 5

13

You cannot send the JSON string as it is - you have to encode it.

There is a Javascript function for that.

var strJSON = encodeURIComponent(JSON.stringify(myJSON));

This should do the trick.

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

4 Comments

tried the fiddle but error, :( I know its really the JSON stuff bcoz when I remove it from the param my code works fine.
What error do you get? 404? Is the recieving page correctly programmed to be able to take this kind of parameters? 404 indicates some kind of rewriteerror. If you need to pass the variable you might need to do domain.com/url?json=thestring. See updated fiddle: jsfiddle.net/EpBjE/1
You're updated fiddle now did not show any 404 error, It shows a blank page. now how Can I get the passed value?
You have to do that from serverside
1

you can use the encodeURIComponent() function encodes a URI component.This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ # Tip: Use the decodeURIComponent() function to decode an encoded URI component.

var jsonURLS = encodeURIComponent(JSON.stringify(myJSON));

1 Comment

still get the same error, mybe its because of the \t\t\t\ts? in the url?
0

You shouldn't pass it as an url parameter. You should use POST to send it. So yout JS code could look like this:

$.ajax({
        type: 'POST',
        data: strJSON,
        contentType: 'application/json',
        url: yourMethodUrl
    }

I don't know how your server method is written but you might update sometihng in there too so it won't try to read url and use request's values instead.

3 Comments

Ive tried this but I cant use this method since I just need the parameters to be passed I dont need it to throwback the data.
I'm not sure what you mean. POST is used exactly to pass data to whatever it's calling. Whenever a client needs to pass anything to the server, the POST verb is being used. When you're trying to send data using url parameters, you actually using the GET verb and it's purpose is to retrieve the data, not to send it. So you're doing exactly the opposite thing than you should. At least, if I understand the problem correctly.
@karol.barkowski, What do I do if I want to send search data to the server but I also want every search to have a unique URL so users can send links of searches to others?
0

Changing from GET to POST it not an innocuous choice. Among another thing, POST preclude HTTP cache usage. Now, considering the initial example that seems to require the generation of a custom document, cache may be of a little help.

Comments

0

Did you get the convenient solution(s)?

If not, I recommend to make small change:

  • AS-IS: localhost/accounting/generate_pdf//pdf

  • TO-BE: localhost/accounting/generate_pdf?json=

    • Assumption: your-JSON encodeURIComponent(JSON.stringify(myJSON));
    • Additional code: you must change a few line of code at server-side to handle $_GET['json']

Hope that helps

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.