English 中文(简体)
页: 1 Qu SON
原标题:jQuery ajax parse JSON data error

我正在讨论一个<代码>。 UncaultyntaxError: Unexpected end of content at the following Code.

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);

我正在尝试寻找解决办法。 这是我采取的步骤。

Ensuring the JSONdata is correct - checked via http://www.jsonlint.com/ and Ensuring that I have closed all brackets

JSON数据格式如下:

[{"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]}]

我这样做的另一点是,我正在使用造成错误的原型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 

我通过论坛进行搜索,发现解决办法是将Jajax美元改为“Query.ajax”,然而,此后,“代码” UncaultyntaxError: Unexpected end of content差错。

在这个问题上给予任何帮助。 问题是什么想法?

最佳回答

您的服务器是否具体规定了响应类型/json? 如果是的话, j子会自动放弃结果,甚至收回。 顺便说一句,你再次试图 par平已经教过的东西。

Try specifying the dataType in your call, and jQuery will pre-parse the result for you.

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.
});
问题回答

I had the same issue, but with a different root cause. in my API controller, I had

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");

这使我的神学方法提供了它希望得到的预计投入。

如你需要使用JQuery snoConflict(),以便原型能够保留$

例:

<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>

文件:http://api.jquery.com/jQuery.noConflict/"rel=“nofollow”http://api.jquery.com/jQuery.noConflict/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签