游.程 2016-04-06 20:48 采纳率: 0%
浏览 33

关于杰森

I am currently using javascript and trying to pass information as query string....i am able to send data in query string through javascript but want to pass in JSON format.....

Ex: url?[{"Key":"value"},{"Key":"value"}]

So that i can extract it...

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
    function call()

    {
        var name = document.getElementById("uname").value;
        var file = document.getElementById("file").value;
        alert(name + file);
    }
    var xhttp  = new XMLHttpRequest();

    xhttp.onreadystatechange = function()
   {
      if(xhttp.status == 200 && xhttp.readyState == 4)
      {
          var response =  xhttp.responseText;

          document.getElementById("demo").innerHTML = response ;
      }
      xhttp.open("GET","http://127.0.0.1/Jarvis/json.html?name="+JSON.parse(response)+"&file="+file+"&submit=Submit",true);
      xhttp.send();
   }



    </script>
</head>
<body>

<form action="" method="get" >

<input type="text" name="name" id="uname"/><br/><br/>
<input type="file" name="file"id="fname" size="50"/><br/><br/>
<input type="submit" name="submit" value="Submit"/>

</form>

<p id="demo"></p>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33736048 2016-04-06 20:53
    关注

    Since you want to put it in a URL you'll want to be sure the characters are URL-safe. Easiest way to do that is to invoke encodeURIComponent on the JSON string (which itself can be generated by calling JSON.stringify and passing in the desired object), then plug the resulting value into a query string parameter.

    On the other end, when you want to translate the query string value to an object, invoke decodeURIComponent on the value before passing it to JSON.parse.

    评论

报告相同问题?