javascript jQuery ajax 解析 JSON 数据错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6808492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax parse JSON data error
提问by Queryer
I am encountering a Uncaught SyntaxError: Unexpected end of input
at the following code.
我Uncaught SyntaxError: Unexpected end of input
在以下代码中遇到了一个。
var dataURL = "LineChartController?tp=" + tpAtt + "&dept=" + dept + "&date=" + dateMY;
alert(dataURL);
var JSONdata = jQuery.ajax({
type: "GET",
url: dataURL,
async: false
}).responseText;
var psSeriesData2 = JSON.parse(JSONdata);
I am tried looking around and found no solution. These are the steps that I have taken.
我试图环顾四周,但没有找到解决方案。这些是我采取的步骤。
Ensuring the JSONdata is correct - checked via http://www.jsonlint.com/and Ensuring that I have closed all brackets
确保 JSONdata 正确 - 通过http://www.jsonlint.com/检查并确保我已关闭所有括号
The JSONdata is in the following format:
JSONdata 采用以下格式:
[{"Dates":["1-10","2-10","3-10","4-10","5-10","6-10","7-10","8-10"],"psScores":[78.78787878787878,79.7979797979798,78.78787878787878,78.78787878787878,78.78787878787878,79.7979797979798,79.7979797979798,76.92307692307693]}]
Another thing I did is that I am using prototype.js and other javascript libraries that cause an error,
我做的另一件事是我使用了prototype.js和其他导致错误的javascript库,
Uncaught TypeError: Object function $(element) {
if (arguments.length > 1) {
for (var i = 0, elements = [], length = arguments.length; i < length; i++)
elements.push($(arguments[i]));
return elements;
}
if (Object.isString(element))
element = document.getElementById(element);
return Element.extend(element);
} has no method 'ajax'
I searched through forum and found that the solution is to change the $.ajax to jQuery.ajax, however, after that, the Uncaught SyntaxError: Unexpected end of input
error appeared.
找了论坛,发现解决办法是把$.ajax改成jQuery.ajax,但是之后就Uncaught SyntaxError: Unexpected end of input
出现了这个错误。
Appreciate any help on this issue. Any ideas what the problem is?
感谢有关此问题的任何帮助。任何想法是什么问题?
回答by James Fleming
I had the same issue, but with a different root cause. in my API controller, I had
我遇到了同样的问题,但根本原因不同。在我的 API 控制器中,我有
public HttpResponseMessage Invite(Invitation invitation)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
The solution to the problem was to add a response message:
问题的解决方法是添加响应消息:
var response = Request.CreateResponse(HttpStatusCode.OK, "invitation sent");
This gave my ajax method the expected input it was hoping to parse.
这为我的 ajax 方法提供了它希望解析的预期输入。
回答by AlienWebguy
Sounds like you need to apply JQuery's noConflict()
so that Prototype can keep the $
听起来您需要应用 JQuerynoConflict()
以便 Prototype 可以保持$
Example:
例子:
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
// Code that uses other library's $ can follow here.
</script>
Documentation: http://api.jquery.com/jQuery.noConflict/
回答by PersonThing
Is your server specifying a response type of application/json? If so, jQuery will automatically parse the result before it even gets back. Meaning, you're trying to parse what's already been parsed.
您的服务器是否指定了 application/json 的响应类型?如果是这样,jQuery 会在结果返回之前自动解析结果。意思是,您正在尝试解析已经解析的内容。
Try specifying the dataType in your call, and jQuery will pre-parse the result for you.
尝试在您的调用中指定 dataType,jQuery 将为您预解析结果。
You should also try to do it asynchronously if possible.
如果可能,您还应该尝试异步执行此操作。
var dataURL = "LineChartController?tp=" + tpAtt + "&dept=" + dept + "&date=" + dateMY;
var JSONdata = jQuery.ajax({
type: "GET",
dataType: "json",
url: dataURL
}).done(function(jsonData){
// do something with the data, it should already be parsed
alert(jsonData.length); // your data sample is an array, see if it gets a length back
}).fail(function(xhr){
// uh oh, we failed.. you should always handle failures too.
});