drydaenth257216154
drydaenth257216154
2014-03-22 23:09
浏览 111

我的JSON字符串无法通过HTTP GET请求获得完整

when i send my array to my php file it incomplete recive to php file. my arr varible is :

[["╪▒┘à┘╛┘╪د","67126881188552864","Empty,Empty,Empty,Empty,8644,-360,-4,8691,-3
.48,-313,1015,4.334 M,1392/12/28,12:30:08,3.99 M,10 B,10%,530,16.4,16.38,┘à╪ش╪د╪
▓,7,3130,8644,1,50,5000, , , ,8644,11553,359,307,8645,1,1,8655,11
030,2,┌»╪▒┘ê┘ç ┘à┘╛┘╪د (╪│┘ç╪د┘à┘è ╪╣╪د┘à) (╪▒┘à┘╛┘╪د),╪ذ╪د╪▓╪د╪▒ ╪د┘ê┘ (╪ز╪د
╪ذ┘┘ê┘è ╪د╪╡┘┘è) ╪ذ┘ê╪▒╪│ ,connOK"]]

i send data with HTTP GET request like below

    var myJsonString = JSON.stringify(arr);

    page.open('http://localhost/bptest/danger/get.php?data='+myJsonString,function(status){
            console.log("Page opened");
            if (status !== 'success') {
                console.log('FAIL');
            } else {
                console.log("REPLY FROM SERVER:");
                console.log(page.content);
            }});

end my simple php file is below code :

<li><?php echo $_GET["data"]; ?></li>

and php get's this :

[["?????","67126881188552864","Empty,Empty,Empty,Em
pty,8644,-360,-4,8691,-3.48,-313,1015,4.334 M,1392/12/28,12:30:08,3.99 M,10 B,10
%,530,16.4,16.38,????,7,3130,8644,1,50,5000,

why it's crash when recive from php and how i can fix this

图片转代码服务由CSDN问答提供 功能建议

当我将我的数组发送到我的php文件时,它不完整地转到了php文件。 它是:

 <代码> [[ “╪▒┘à┘╛┘╪د”, “67126881188552864”,“空,空,空,空,8644,-360,-4,8691, -  3 
.48,-313,1015,4.334 M,1392/12 / 28,12:30:08,3.99 M,10 B,10%,530,16.4,16.38,┘à╪ش╪د╪
  ▓,7,3130,8644,1,50,5000,&安培; NBSP;,&安培; NBSP;,&安培; NBSP;,8644,11553,359,307,8645,1,1,8655,11 
030,2,┌  »╪▒┘ê┘ç┘à┘╛┘╪د(╪│┘ç╪د┘à┘è╪╣╪د┘à)(╪▒┘à┘╛┘╪د),╪ذ╪د╪▓  ╪د╪▒╪د┘ê┘(╪ز╪د
╪ذ┘┘ê┘è╪د╪╡┘┘è)╪ذ┘ê╪▒╪│,connOK“]] 
    
 
 

我使用HTTP GET请求发送数据,如下所示

  var myJsonString = JSON.stringify(arr); 
 
页面。  open('http://localhost/bptest/danger/get.php?data ='+ myJsonString,function(status){
 console.log(“Page opened”); 
 if(status!=='success)  '){  
 console.log('FAIL'); 
} else {
 console.log(“REPLY FROM SERVER:”); 
 console.log(page.content); 
}}); 
 <  / code>  
 
 

结束我的简单php文件在代码下面:

 &lt; li&gt;&lt;?php echo $ _GET [“ 数据”];  ?&gt;&lt; / li&gt; 
   
 
 

和php得到这个:

  [[“???  ??“,”67126881188552864“,”空,空,空,Em 
tyty,8644,-360,-4,8691,-3.48,-313,1015,4.334 M,1392/12 / 28,12:30:  08,3.99 M,10 B,10 
%,530,16.4,16.38,????,7,3130,8644,1,50,5000,
   
 
  

为什么它从php恢复时崩溃以及我如何解决这个问题

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongming5444
    dongming5444 2014-03-23 00:23
    已采纳

    Note the <meta charset="utf-8" /> line (and the file itself should be in UTF-8 which should be the default for any decent text editor) and also the use of encodeURIComponent which is when you want to encode something to fit into the query string (since unlike encodeURI, it also encodes characters like & and =).

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <script>
    var arr = [["╪▒┘à┘╛┘╪د","67126881188552864","Empty,Empty,Empty,Empty,8644,-360,-4,8691,-3\
    .48,-313,1015,4.334 M,1392/12/28,12:30:08,3.99 M,10 B,10%,530,16.4,16.38,┘à╪ش╪د╪\
    ▓,7,3130,8644,1,50,5000,&nbsp;,&nbsp;,&nbsp;,8644,11553,359,307,8645,1,1,8655,11\
    030,2,┌»╪▒┘ê┘ç ┘à┘╛┘╪د (╪│┘ç╪د┘à┘è ╪╣╪د┘à) (╪▒┘à┘╛┘╪د),╪ذ╪د╪▓╪د╪▒ ╪د┘ê┘ (╪ز╪د\
    ╪ذ┘┘ê┘è ╪د╪╡┘┘è) ╪ذ┘ê╪▒╪│ ,connOK"]];
    var myJsonString = encodeURIComponent(JSON.stringify(arr));
    
    page.open('http://localhost/bptest/danger/get.php?data='+myJsonString,function(status){
        console.log("Page opened");
        if (status !== 'success') {
            console.log('FAIL');
        } else {
            console.log("REPLY FROM SERVER:");
            console.log(page.content);
        }
    });
    </script></head><body></body></html>
    

    PHP:

    Note the security risk comment (you could also avoid the risk by using "text/plain" as the content-type but then it won't get rendered as HTML if you need that).

    <?php
    header('Content-type: text/html;charset=utf-8');
    echo $_GET['data']; // You better fix this after testing, as it invites XSS scripting attacks!
    ?>
    
    点赞 评论
  • doutaoer3148
    doutaoer3148 2014-03-22 23:37

    You must use UTF-8 charset with JSON. Check this points:

    • Header charset
    • Encoding php file with UTF-8
    • Check if receveid data are UTF-8 encoded

    I think the charset is not good on one side of your app or is changed during the data transfer.

    点赞 评论
  • drutjkpsr67393592
    drutjkpsr67393592 2014-03-23 00:12

    You must use encodeURI() to encode you json string.

    Please see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI.

    点赞 评论
  • drn5375
    drn5375 2014-03-23 00:21

    Try This

     var myJsonString = JSON.stringify(encodeURI(arr));
    
        page.open('http://localhost/bptest/danger/get.php?data='+myJsonString,function(status){
                console.log("Page opened");
                if (status !== 'success') {
                    console.log('FAIL');
                } else {
                    console.log("REPLY FROM SERVER:");
                    console.log(page.content);
                }});
    
    点赞 评论

相关推荐