admin管理员组

文章数量:1287569

I am encountering a Uncaught SyntaxError: Unexpected end of input 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);

I am tried looking around and found no solution. These are the steps that I have taken.

Ensuring the JSONdata is correct - checked via / and Ensuring that I have closed all brackets

The JSONdata is in the following format:

[{"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,

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.

Appreciate any help on this issue. Any ideas what the problem is?

I am encountering a Uncaught SyntaxError: Unexpected end of input 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);

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./ and Ensuring that I have closed all brackets

The JSONdata is in the following format:

[{"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,

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.

Appreciate any help on this issue. Any ideas what the problem is?

Share Improve this question edited Jan 21, 2014 at 8:19 Vikas Arora 1,6662 gold badges17 silver badges39 bronze badges asked Jul 24, 2011 at 17:38 QueryerQueryer 2592 gold badges5 silver badges16 bronze badges 1
  • 1 What jQuery version are you using? – yoda Commented Jul 24, 2011 at 17:42
Add a ment  | 

3 Answers 3

Reset to default 4

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

This gave my ajax method the expected input it was hoping to parse.

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.

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

Sounds like you need to apply JQuery's noConflict() so that Prototype can keep the $

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./jQuery.noConflict/

本文标签: javascriptjQuery ajax parse JSON data errorStack Overflow