2

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.com/ 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?

1
  • 1
    What jQuery version are you using? Commented Jul 24, 2011 at 17:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This indeed happened to me as well. But I dont understand why your solution actually works (it does work on my end as well)... why would an 'empty' response with 200 not be ok?
I think the empty response is fine, as far as the server is concerned, but the issue is in the client side parsing as near as I can tell.
James, I worked out my issue. A 200 means succes and returns something by specification. If the server wants to communicate a succes and not return something it seems to be more appropriate to use a 209 which means succes and noContent. On the client side, this is supported in jquery. A 200 with some json content, works fine in jquery, a 209 without content also works fine. Hope this helps!
2

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.