17936

jQuery.ajax() call is returning JSON.parse unexpected character error

Question:

I am trying to debug the following jQuery.ajax() call:

var rollnum = $('#rollNum').val() $.ajax({ url: sURL + "myController/myMethod", type: "POST", data: {rollnum: rollnum}, dataType: 'json', success: function(json){alert(json)}, error:function (xhr, ajaxOptions, thrownError){ alert(xhr.status); alert(thrownError);} });

Here is the method:

function myMethod(){ $query = $this->db->query("SELECT tblontario.Address, tblontario.Municipality FROM tblontario WHERE RollNum = 211010002528200"); $json_address = array(); $json_municipality = array(); foreach ($query->result_array() as $row){ $json_address[] = $row['Address']; $json_municipality[] = $row['Municipality']; } $json['address'] = $json_address; $json['municipality'] = $json_municipality; echo json_encode($json); exit; }

Currently what is happening is that I am receiving an error back. The error consists of these details:

xhr.status: 200; and thrownError: SyntaxError: JSON.parse: unexpected character

When I look at the XHR Response tab in firebug it shows:

Reload the page to get source for: http://www.mysite.ca/myController/myMethod

Something seems to be working correctly since we are getting the xhr.status of 200, but I can't seem to retrieve the json data back from this call. Can anyone give me a suggestion for what the problem might be? Thanks.

Answer1:

Set the header of the content to type of json... Here is an example of setting header type.

header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json');

In jQuery 1.4 onwards the JSON data is parsed in a strict manner.

Any malformed JSON is rejected and a parse error is thrown.

Answer2:

The server returns a redirection page with the code 200 instead of one of the redirection codes. The AJAX call doesn't redirect as the status code is 200, and it can't decode the response because it's not JSON.

Either make the server return the correct status code, or request the page as plain text so that you can examine it and look for the redirection message before decoding it.

Answer3:

Switch off BOM (encoding UTF-8 without BOM) in text editor. BOM is a system (not visible) symbols in the begining of UTF8 file.

Recommend

  • PrimeFaces :: display p:Dialog to fill-in a form field, without reposting
  • Wordpress multisite, domain mapping www issue
  • how to convert hijri date to gregorian date using php?
  • Looking up through multiple points of similarity
  • Returning array of int in C [duplicate]
  • Inconsistent results (C)?
  • CodeIgniter: Populating a select input/ form dropdown with database table data
  • Thinking Sphinx and searching multiple models
  • How to get data from a joined table
  • How to use WSO2 AM to call my backend services that are secured with OATH
  • training UBM with sidekit from custom data
  • how to show selected item in edit form_dropdown codeigniter
  • Issue in accessing table contents using status parameter
  • MATLAB: MEX matrix division gives different result than m-file
  • Libspotify playing problems
  • Passing variable to WinBugs model in R
  • Stored data can't access in Yii
  • Is there a MySQL query that can encode to JSON in exactly this way?
  • FCM on app closed
  • Ubuntu and bcrypt
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Obtain ObjectIdHex value from mgo query
  • Controls, properties, events and timers running in design time
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • How do I rollback to a specific git commit
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • json Serialization in asp
  • using HTMLImports.whenReady not working in chrome
  • Can't mass-assign protected attributes when import data from csv file
  • Unable to use reactive element in my shiny app