I currently have a code snippet which I'd like to use to get Ajax Calls and return JSON data from my PHP file which I can use however I want in the jQuery side. My problem is that IF I change the data type to JSON, I always get error on the request, HOWEVER when I inspect it in Firebug I can see that the PHP file has just returned the JSON values fine!
我目前有一个代码片段,我想用它来获取Ajax调用并从我的PHP文件返回JSON数据,我可以在jQuery端使用它。我的问题是,如果我将数据类型更改为JSON,我总是会在请求中出错,但是当我在Firebug中检查它时,我可以看到PHP文件刚刚返回JSON值!
This is the HTML:
这是HTML:
<form id="formm" method="post">
<input type="text" name="test" value="" id="test"/>
<input type="submit" name="submit" value="Submit"/>
</form>
<div id="result"></div>
This is the JS:
这是JS:
$("#formm").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
/* Clear result div*/
$("#result").html('');
/* Get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "functions.php",
type: "post",
dataType: "json",
data: values,
success: function(data) {
alert(data);
},
error:function(){
alert("failure");
$("#result").html('There is error while submit');
}
});
});
PHP file:
PHP文件:
echo json_encode(array('returned_val' => $_POST['test']));
When I inspect with Firebug I get: returned_val "whatever I type in the textbox". Can anyone tell me what could be the problem?
当我用Firebug检查时,我得到:returned_val“无论我在文本框中输入什么”。谁能告诉我可能是什么问题?
UPDATE:
更新:
Response Headers:
响应标题:
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language ***
Content-Length 5
Content-Type application/json; charset=utf-8
Host localhost
Referer http://localhost/test/
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
X-Requested-With XMLHttpRequest
When I put the errors in the console, I get this:
当我把错误放在控制台中时,我得到了这个:
The following error occured: parsererror SyntaxError: JSON.parse: unexpected character
发生以下错误:parsererror SyntaxError:JSON.parse:意外字符
1 个解决方案
#1
6
The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead
JSON数据以严格的方式解析;任何格式错误的JSON都会被拒绝,并抛出一个解析错误。从jQuery 1.9开始,空响应也被拒绝;服务器应该返回null或{}的响应
here is the documentation http://api.jquery.com/jQuery.ajax/
这里是文档http://api.jquery.com/jQuery.ajax/
so check your json response from php (whether the json data is well formed and non empty)
所以从php检查你的json响应(json数据是否格式正确且非空)
#1
6
The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead
JSON数据以严格的方式解析;任何格式错误的JSON都会被拒绝,并抛出一个解析错误。从jQuery 1.9开始,空响应也被拒绝;服务器应该返回null或{}的响应
here is the documentation http://api.jquery.com/jQuery.ajax/
这里是文档http://api.jquery.com/jQuery.ajax/
so check your json response from php (whether the json data is well formed and non empty)
所以从php检查你的json响应(json数据是否格式正确且非空)