jQuery JSONP解析错误

时间:2014-06-22 08:33:42

标签: jquery ajax jsonp

我正在开发一个使用jQuery AJAX从我们的在线应用程序获取数据的PhoneGap项目。

以下是我正在使用的代码:

$.ajax({
    type: 'GET',
    url: home ,
    async: false,
    dataType: "jsonp",
    jsonpCallback: 'jsonCallback',
    data: {action: 'get_survey', survey: survey_id},
    success: function (result) {

        $.mobile.loading( 'hide', {});
        display_survey(result);

    },
    error: function (request,error) {

        $.mobile.loading( 'hide', {});
        alert(error);
    }
});

以下是从服务器返回的数据:

jsonCallback({"symptoms":{"ent":["Does not pay attention to details or makes careless mistakes wiht, for example, homework","Has difficulty keeping attention to what needs to be done","Does not seem to listen when spoken to directly","Does not follow through when given directions and fails to finish activities (not due to refusal or vailure to understand)","Has difficulty organizing tasks and activities","Avoids, dislikes, or does not want to start tasks that require ongoing mental effort","Loses things necessary for tasks or activities (toys, assignments, pencils or books","Is easily distracted by noises or other stimuli","Is forgetful in daily activities","Fidgets with hands or feet or squirms in seat","Leaves seat when remaining seated is expected","Runs about or climbs too much when remaining seated is expected","Has difficulty playing or beginning quiet play activities","Is 'on the go' or often acts if 'driven by a motor'","Talks too much","Blurts out answers before questions have been completed","Has difficulty waiting his or her turn","Interrupts or intrudes on others' conversations and\/or activities","Argues with adults","Loses temper","Actively defies or refuses to go along with adults' requets or rules","Deliberately annoys people","Blames others for his or her mistakes or misbehaviors","Is touchy or easily annoyed by others","Is angry or resentful","Is spiteful and wants to get even","Bullies, threatens, or intimidates others","Starts physical fights","Lies to get out of trouble or to avoid obligations (ie, 'cons' others)","Is truant from school (skips school) without permission","Is physically cruel to people","Has stolen things that have value","Deliberately destroys others' property","Has used a weapon that can cause serious harm (bat, knife, brick, gun)","Is physically cruel to animals","Has deliberately set fires to cause damage","Has broken into someone else's home, business or car","Has stayed out at night without permission","Has run away from home overnight","Has forced someone into sexual activity","Is fearful, anxious or worried","Is afraid to try new things for fear of making mistakes","Feels worthless or inferior","Blames self for problems, feels guilty","Feels lonely, unwanted or unloved; complains that 'no one loves him or her'","Is sad, unhappy or depressed","Is self-conscious or easily embarrrassed"]},"performance":{"ent":["Overall school performance","Reading","Writing","Mathematics","Relationship with parents","Relationship with siblings","relationship wiht peers","Participation in organized activities (eg, teams)"]}})

现在,我正在浏览器中测试该应用。从与在线应用程序相同的服务器调用PG应用程序时,它可以正常工作。但是,如果PG应用程序位于不同的服务器上,我会从jQuery获得'parseError'并触发错误函数。

但是,当我查看控制台时,我可以看到PG应用程序正在接收所有数据。

我在设置JSONP呼叫时是否遗漏了某些内容?

2 个答案:

答案 0 :(得分:1)

我认为您需要更改

jsonpCallBack:'jsonpCallback'

位到

jsonpCallBack: function() { alert('foo'); }

答案 1 :(得分:0)

您的JSON是有效的。看起来像跨域请求问题。 从你的帖子中观察:

请删除或更改

 async : false 

 async : true

因为默认情况下,所有请求都是异步发送的(即默认情况下设置为true)。如果需要同步请求,请将此选项设置为false。跨域请求和 dataType:" jsonp" 请求不支持同步操作。请参阅here