简体   繁体   English

jQuery将不接受json_encode()数据

[英]jquery won't accept json_encode() data

I'm using the following piece of code to send data from a form to a php script that will do some calculations using a class: 我正在使用以下代码将数据从表单发送到php脚本,该脚本将使用类进行一些计算:

$.ajax({
    type: "POST",
    url: "test2.php",
    data: values,
    success: function(returnedData) {
        $("#result").html(returnedData);
    },
    error: function(){
        alert("failure");
        $("#result").html('error while submitting');
    }
});

My PHP script creates an array which I encode with JSON and then echo 我的PHP脚本创建了一个数组,该数组使用JSON进行编码,然后回显

$jsonString = array(
    'monthText' => $Month->getMonthText($month),
    'gross' => $formatGross,
    'net' =>  $formatNet
    );
echo json_encode($jsonString);

So far so good. 到现在为止还挺好。 But I'm not very keen on displaying the raw JSON. 但是我不太热衷于显示原始JSON。 I'd like to format the text before it's written to the document. 我想先将文本格式化,然后再将其写入文档。 I've tried $.parseJSON() and $.getJSON() but none of them work. 我已经尝试了$.parseJSON()$.getJSON()但是它们都不起作用。 The jQuery docs says it needs a string but isn't json_encode() making my array to a string? jQuery文档说它需要一个字符串,但是不是json_encode()使我的数组成为字符串吗? Isn't echo making it a string? 是不是echo使它成为字符串? Why am I getting the error Uncaught SyntaxError: Unexpected token { in my jQuery-file? 为什么我的jQuery文件中出现错误Uncaught SyntaxError: Unexpected token { My theory is that it doesn't have single quotes around it. 我的理论是,它周围没有单引号。

I've tried using header('Content-type: application/json') in my PHP script and I've also tried dataType: json in my AJAX POST but nothing is working. 我试过在PHP脚本中使用header('Content-type: application/json') ,也尝试在AJAX POST中使用dataType: json ,但没有任何效果。

What am I doing wrong? 我究竟做错了什么?

From the comments it sounds like you are using json_encode wrong. 从注释中听起来您使用的是json_encode错误。 Store each piece of data into an array and then encode the array. 将每条数据存储到一个数组中,然后对该数组进行编码。

$data = array();
for($i=0;$i<$something;$++) {
   $data[] = array(
      'monthText' => $Month->getMonthText($month),
      'gross' => $formatGross,
      'net' =>  $formatNet
    );
}

echo json_encode($data);

Then your Javascript would need to be 然后您的Javascript需要

$.ajax({
    type: "POST",
    url: "test2.php",
    dataType:"json",
    data: values,
    success: function(returnedData) {
        //returnedData will now be an array Object
        for( var i=0; i<returnedData.length; i++ ) {
           $("#result").html(returnedData[i].monthText);
        }
    },
    error: function(){
        alert("failure");
        $("#result").html('error while submitting');
    }
});

对于解析,请使用jQuery.parseJSON(returnedData);

Add dataType in your ajax call like this. 像这样在您的ajax调用中添加dataType。

$.ajax({
    type: "POST",
    dataType:"json",
    url: "test2.php",
    data: values,
    success: function(returnedData) {
        $("#result").html(returnedData);
    },
    error: function(){
        alert("failure");
        $("#result").html('error while submitting');
    }
});

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

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