Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help guys.

I am trying to fetch data from the server which is returning JSON data but I am getting the following error message (in Firefox 48).


Uncaught SyntaxError: Unexpected token <

It seems like the JSON data that is returned is invalid or its not in the expected format.

When I run it in Chrome 52, there is no error message and the data is being returned on the response but it does not run the code inside my function.

My server is returning data in the following JSON format.

C#
[{ "ActionCardId": 1, "ActionCardNumber": "279877", "AccountNo": "2202322179", "ClientName": null, "CellNumber": "0787596971", "TenantName": null, "TenantCellNumber": null, "AddressStandNo": null, "AddressStreet": "43 DELMAS AVENUE", "AddressSuburb": "BOKSBURG", "AddressCity": null, "AddressPortion": null, "AddressEarthNo": null, "AddressPremise": null, "AddressGISKEY": null, "AmountDueTotal": 4080.0, "AmountDue30Day": null, "AmountDueCurrent": null, "AmountDue60Day": null, "ServiceLevy": null, "MeterNumberWater": null, "MeterNumberElectricity": "M22541", "MeterNumberElectricity2": null, "MeterNumberElectricity3": null, "MeterNumberPrePaid": null, "IsPrinted": true, "IsResidential": null, "ActionSuccessful": false, "ActionCardDate": "2016-08-22T00:00:00", "Remarks": null, "UnSuccessfulReason": null, "CaptureContractorId": null, "AddressCombo": ", 43 DELMAS AVENUE, BOKSBURG, , ", "BillCycleName": "120", "ServiceIndicator": null, "WaterReading": null, "WaterReadingDate": null, "ElectricityReading": null, "ElectricityReadingDate": null, "Unit": null, "ActionCardTypeId": 2, "ContractorId": 1, "ActionCardStatusId": 1, "UnSuccessfulActionReasonId": null, "TechnicianId": 1, "AccountInfoId": null, "ServiceDeliveryCentreId": 4, "BillCycleId": 2, "ActionCardType": null, "Contractor": null, "ActionCardStatus": null, "UnSuccessfulActionReason": null, "Technician": null, "AccountInfo": null, "ServiceDeliveryCentre": null, "BillCycle": null, "ActionName": "ED", "DateCreated": "2016-07-06T00:00:00", "DateModified": "2016-08-13T10:24:17.5833635", "CreatedByOnlineUserId": null, "ModifiedByOnlineUserId": "fa93e65d-e4a7-4a0c-91c6-4561e2612f9c", "CreatedByUser": null, "ModifiedByUser": null }, { "ActionCardId": 2, "ActionCardNumber": "279876", "AccountNo": "2200630592", "ClientName": null, "CellNumber": "0787596971", "TenantName": null, "TenantCellNumber": null, "AddressStandNo": null, "AddressStreet": "43 DELMAS AVENUE", "AddressSuburb": "BOKSBURG", "AddressCity": null, "AddressPortion": null, "AddressEarthNo": null, "AddressPremise": null, "AddressGISKEY": null, "AmountDueTotal": 4080.0, "AmountDue30Day": null, "AmountDueCurrent": null, "AmountDue60Day": null, "ServiceLevy": null, "MeterNumberWater": null, "MeterNumberElectricity": "M22541", "MeterNumberElectricity2": null, "MeterNumberElectricity3": null, "MeterNumberPrePaid": null, "IsPrinted": true, "IsResidential": null, "ActionSuccessful": false, "ActionCardDate": "2016-08-22T00:00:00", "Remarks": null, "UnSuccessfulReason": null, "CaptureContractorId": null, "AddressCombo": ", 43 DELMAS AVENUE, BOKSBURG, , ", "BillCycleName": "120", "ServiceIndicator": null, "WaterReading": null, "WaterReadingDate": null, "ElectricityReading": null, "ElectricityReadingDate": null, "Unit": null, "ActionCardTypeId": 2, "ContractorId": 1, "ActionCardStatusId": 1, "UnSuccessfulActionReasonId": null, "TechnicianId": 1, "AccountInfoId": null, "ServiceDeliveryCentreId": 4, "BillCycleId": 2, "ActionCardType": null, "Contractor": null, "ActionCardStatus": null, "UnSuccessfulActionReason": null, "Technician": null, "AccountInfo": null, "ServiceDeliveryCentre": null, "BillCycle": null, "ActionName": "ED", "DateCreated": "2016-07-06T00:00:00", "DateModified": "2016-08-13T10:51:16.575637", "CreatedByOnlineUserId": null, "ModifiedByOnlineUserId": "fa93e65d-e4a7-4a0c-91c6-4561e2612f9c", "CreatedByUser": null, "ModifiedByUser": null }, { "ActionCardId": 3, "ActionCardNumber": "279875", "AccountNo": "2200630306", "ClientName": null, "CellNumber": "0787596971", "TenantName": null, "TenantCellNumber": null, "AddressStandNo": null, "AddressStreet": "43 DELMAS AVENUE", "AddressSuburb": "BOKSBURG", "AddressCity": null, "AddressPortion": null, "AddressEarthNo": null, "AddressPremise": null, "AddressGISKEY": null, "AmountDueTotal": 4080.0, "AmountDue30Day": null, "AmountDueCurrent": null, "AmountDue60Day": null, "ServiceLevy": null, "MeterNumberWater": null, "MeterNumberElectricity": "M22541", "MeterNumberElectricity2": null, "MeterNumberElectricity3": null, "MeterNumberPrePaid": null, "IsPrinted": true, "IsResidential": null, "ActionSuccessful": true, "ActionCardDate": "2016-08-22T00:00:00", "Remarks": null, "UnSuccessfulReason": null, "CaptureContractorId": null, "AddressCombo": ", 43 DELMAS AVENUE, BOKSBURG, , ", "BillCycleName": "120", "ServiceIndicator": null, "WaterReading": null, "WaterReadingDate": null, "ElectricityReading": null, "ElectricityReadingDate": null, "Unit": null, "ActionCardTypeId": 2, "ContractorId": 1, "ActionCardStatusId": 1, "UnSuccessfulActionReasonId": null, "TechnicianId": 1, "AccountInfoId": null, "ServiceDeliveryCentreId": 4, "BillCycleId": 2, "ActionCardType": null, "Contractor": null, "ActionCardStatus": null, "UnSuccessfulActionReason": null, "Technician": null, "AccountInfo": null, "ServiceDeliveryCentre": null, "BillCycle": null, "ActionName": "ED", "DateCreated": "2016-07-06T00:00:00", "DateModified": "2016-08-13T10:51:16.6757162", "CreatedByOnlineUserId": null, "ModifiedByOnlineUserId": "fa93e65d-e4a7-4a0c-91c6-4561e2612f9c", "CreatedByUser": null, "ModifiedByUser": null }]


What could be the issue here?

I am using jsonp because I am using cross domain.
Thank you.

What I have tried:

JavaScript
$("#importDailyJobs").click(function () {

        var technicianId = 1;

        var serverUrl = "http://localhost:35825/Mobile/ImportDailyReadings"

        $.get(serverUrl, { TechnicianId: technicianId }, function (data, status) {

            alert("Success");
            console.log(data);


        }, "jsonp");

    });
Posted
Updated 29-Aug-16 21:59pm

1 solution

Error is in your ajax request, you are not mentioned in your ajax request which type of data you are getting in response. I Think if you make request like this request mention below, your error will be resolved.

JavaScript
$("#importDailyJobs").click(function () {
        var technicianId = 1;
 
        var serverUrl = "http://localhost:35825/Mobile/ImportDailyReadings"
    $.ajax({
             url: serverUrl ,
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             jsonp: 'jsonp',
             data: {
                 TechnicianId: technicianId
             },
             success: function(data) {
                alert("Success");
                console.log(data);

             }
         });
});



NOTE:
By default ajax request is get and you should have returned response in JSON formate.
 
Share this answer
 
Comments
Mramba 30-Aug-16 5:42am    
@Asad Raza Yazdani, thank you so much for your help. When I use your solution I am getting a different error message that says "XMLHttpRequest cannot load http://localhost:35825/Mobile/ImportDailyReadings?TechnicianId=1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5754' is therefore not allowed access."

I think I am getting this error because the applications are on different domains (same local host but different ports).

Is there any other way of fetching the data using ajax on cross domains?

Thanks
Asad Raza Yazdani 1-Sep-16 7:49am    
For getting your desire result you will have to set "Access-Control-Allow-Origin" settings in your webconfig file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900