数据类型:" JSON"将无法正常工作 [英] dataType: "json" won't work

查看:134
本文介绍了数据类型:" JSON"将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发回多个变量,从一个PHP文件中的数组使用JSON到阿贾克斯。在code在PHP文件中完美的作品,并做一切与我的数据库,像它应该。但只要我添加数据类型:在AJAXJSON,什么都不会发生在php文件了。我用Google搜索了一下,有些人提到它可能是一个浏览器的问题,但到目前为止,并没有在任何火狐,Chrome或IE浏览器。我使用jQuery的最新版本。

I'm trying to send back multiple variables from a php file to ajax using json in an array. The code in the php file works perfectly and does everything with my database like it should. But as soon as I add dataType: "json" in ajax, nothing happens in the php file anymore. I googled a bit and some people mentioned it could be a browser problem, but so far it doesn't work in either firefox, chrome or IE. I'm using the latest version of jQuery.

这是PHP里面发生了什么:

This is what happens inside php:

<?php
//Create variables and update database

echo json_encode(array("id" => "$realid", "un" => "$username", "date" => "$date"));
?>

这是阿贾克斯code:

And this is the ajax code:

.ajax(
{
   url: 'UpdateComments.php',
   type: 'POST',
   dataType: "json",
   data: 
   {
      type: "add",
      comment: $("#comment").val(),
      id: videoID  
   },
   success: function (data) 
   {
       //Get the data variables from json and display them on page
   }
});

我一无所知这一点,任何建议将不胜AP preciated!

I'm clueless on this, any advice would be greatly appreciated!

推荐答案

常见的问题是JSON在此之前,浏览器打印别的东西是否是一些可读或读取(不可见)字符。尝试做这样的事情:

Common issue is that browser prints "something else" before JSON whether that is some readable or unreadable(invisible) char. Try doing something like this:

<?php
//at the very beginning start output buffereing
ob_start();

// do your logic here

// right before outputting the JSON, clear the buffer.
ob_end_clean();

// now print
echo json_encode(array("id" => $realid, "un" => $username, "date" => $date));
?>

现在,所有的补充数据(JSON之前)将被抛弃,你应该有它的工作...

Now, all supplement data (before JSON) will be discarded and you should have it working...

这篇关于数据类型:&QUOT; JSON&QUOT;将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆