简体   繁体   中英

json parse error coming from ajax post call

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM