简体   繁体   English

检索URL中编码的JSON

[英]Retrieve JSON encoded in URL

I'm using JSON format to send some data between two pages. 我正在使用JSON格式在两个页面之间发送一些数据。 In the second page I need to get the JSON i have encoded in the URL to add some data in it and send it again to a 3rd page. 在第二页中,我需要获取我已在URL中编码的JSON,以便在其中添加一些数据并将其再次发送到第三页。 this is the code I used to send the JSON using GET to the second page. 这是我用来使用GET将JSON发送到第二页的代码。

    form.submit( function(e) {
        // stop the regular form submission
        e.preventDefault();

        // collect the form data
        var data = {};

        data["valueDimensional"] = $('#valueDimensional').val();
        data["timeSlot"] = $('#timeSlot option:selected').val();
        data["splitOption"] = $('#splitOption option:selected').val()

        var strJSON = JSON.stringify(data);
        var escapedStrJSON = encodeURIComponent(strJSON);
        var url = "/warning/app/data?par="+escapedStrJSON;
        window.location.href = url;

     });

In the second page i tried this: 在第二页中,我尝试了此操作:

     form.submit( function(e) {
            // stop the regular form submission
            e.preventDefault();

            $.getJSON(window.location.href, function(data) {
                //data is the JSON string
                alert(data);
            });

        });

在第二页代码中使用JSON.parse(window.location.href)代替:

$.getJSON(window.location.href, function(data) {

getJSON takes a URL and makes an HTTP request to it. getJSON接受一个URL并对其进行HTTP请求。

It does not search through the URL in order to try to find something that looks like JSON encoded directly into it. 它不会搜索URL来尝试查找直接以JSON编码的内容。

You need to: 你需要:

  1. parse the query string to read the value of par 解析查询字符串以读取par的值
  2. Pass the result through JSON.parse . 通过JSON.parse传递结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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