从 Javascript 在 URL 中传递 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18912454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:39:19  来源:igfitidea点击:

Passing JSON in URL from Javascript

javascriptphpjsoncodeigniterurl

提问by Taw

I have a data extracted from a textareawhich is something like this:

我有一个从 atextarea中提取的数据,如下所示:

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

Error_404 页面

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

我需要在 param url 中传递字母,这就是它在 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

我怎样才能做到这一点?我是 JSON 的新手。谢谢

UPDATE

更新

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

执行此操作后,var myJSON = encodeURIComponent(JSON.stringify(myJSON1));
这里是新的 URL,但仍然出现相同的错误

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

回答by larsemil

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

您不能按原样发送 JSON 字符串 - 您必须对其进行编码。

There is a Javascript function for that.

有一个 Javascript 函数。

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

This should do the trick.

这应该可以解决问题。

回答by karol.barkowski

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

您不应将其作为 url 参数传递。您应该使用 POST 发送它。所以你的 JS 代码可能是这样的:

$.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.

我不知道您的服务器方法是如何编写的,但您也可能会更新其中的某些内容,因此它不会尝试读取 url 并使用请求的值。

回答by Ganesh

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.

您可以使用 encodeURIComponent() 函数对 URI 组件进行编码。此函数对特殊字符进行编码。此外,它还对以下字符进行编码: , / ? : @ & = + $ # 提示:使用 decodeURIComponent() 函数解码编码的 URI 组件。

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

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

回答by Emmanuel

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.

从 GET 更改为 POST 它不是一个无害的选择。此外,POST 排除了 HTTP 缓存的使用。现在,考虑到似乎需要生成自定义文档的初始示例,缓存可能会有所帮助。