2

hi i am developing a cordova 2.7.0 phonegap application .i am posting restaurant id to web controller in order to get details of that restaurant.backend is in zend framework but response is calling error functio,the error showing-json parse error and unexpected token < .i am totally confused .please help me.

thing i have already tried: iwhen i change url it changes error name,so error can be in url,i am not sure if it is a cross domain issue.

this is my code

function somefunction() 
{
    alert("called");
    var resid = sessionStorage.getItem("ids");
    alert(resid);
    $.ajax({
        url : 'http://app.mywebsitename.com/takeaway/detail',
        cache : false,

        type : 'GET',
        dataType : 'json',

        crossDomain : true,
        success : function getRestaurantSuccess(data, status) {
        alert("success");           },
        error : function(jqXHR, textStatus, errorThrown) {

            alert(textStatus);
            alert(errorThrown);
        }
    });
}
Jai
  • 74,255
  • 12
  • 74
  • 103
preeti jakhar
  • 25
  • 1
  • 5
  • have you configured `access origin` in config.xml? – QuickFix Mar 04 '14 at 09:33
  • Does this answer your question? ["SyntaxError: Unexpected token < in JSON at position 0"](https://stackoverflow.com/questions/37280274/syntaxerror-unexpected-token-in-json-at-position-0) – miken32 May 29 '21 at 23:33

2 Answers2

0

You have to change two things here:

you have to change dataType:'json', to dataType:'jsonp'

and change your success function

success : function getRestaurantSuccess(data, status) {

to this

success : function (data, status) {

since your url seems to do cross domain request and make sure that response type is json which has to come from your serverside.


Found in your post i am posting restaurant id to web controller
I have not found that you are sending this in your ajax function. so your final code should be:

function somefunction() {
    alert("called");
    var resid = sessionStorage.getItem("ids");
    alert(resid);
    $.ajax({
        url: 'http://app.mywebsitename.com/takeaway/detail',
        cache: false,
        data: {resid : resid}, // pass the res id here
        type: 'GET',
        dataType: 'jsonp', // change to jsonp for cross domain
        crossDomain: true,
        success: function (data, status) { // update your success function
            alert("success");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(textStatus);
            alert(errorThrown);
        }
    });
}
Jai
  • 74,255
  • 12
  • 74
  • 103
0

Error: "showing-json parse error and unexpected token <" that mean: data format response from server incorrect. In client, you set dataType is 'json' but into data response is not json data or may be xml (i not sure, you should check again), ajax parsing data and find '<' into data response from server.

Hanh Le
  • 894
  • 6
  • 14