如何在phonegap中将JSON数据发布到服务器

时间:2015-01-04 09:50:50

标签: json cordova

嗨,我写这段代码,但我得到错误,如UNdefined我们如何解决这个问题,如果有人知道这个答案,请告诉我知道

$.ajax({ type: "POST", contentType:"application/json; charset=utf-8", dataType: "application/json", url:"some url",

    data :{familyVoterId:'0',boothId:'0',voterId:'59136604',previousEnrollmentNumber:'29422415',casteId:'62',street:'hamlet',voterRelationId:'8',cadrePrevYear:'',relativeName:'DESSAPPA',previousRollesList:[{fromDateStr:'2012-10-24',cadreRoleId:'1',cadreCommitteeId:'1',cadreCommitteeLevelId:'1',toDateStr:"2013-10-24"}],partyMemberSinceStr:"2001-10-24",uniqueKey:"10d11073-a8b1-4764-bf2f-e2c0a269e9cd",nameType:"Voter",panchayatId:"0",age:28,surveyTimeStr:"2014-10-24 15:05:15",gender:"M",relationTypeId:8,bloodGroupId:3,dobStr:"1968-02-02",nomineeGender:"1",photoType:"VOTER",voterCardNumber:"UXN0862806",longititude:"78.413",aadheerNo:"987654321",candidateAadherNo:"12345678910",educationId:"2",nomineeAge:"21",previousParicaptedElectionsList:[{"electionTypeId":17,candidateId:"30126",constituencyId:"461"}],voterName:"RAMAKRISHNA",occupationId:"1",houseNo:"3-60",cadreFamilyDetails:[],createdUserId:"152",refNo:"TR-T-9999-12985",constituencyId:"282",latitude:"17.4278",nomineeName:"nominee",mobileNumber:"9999999999"},

    success: function(data)
    {

        alert("success");
    },

    error: function(msg)
    {
        alert(msg.message);
    }
});

3 个答案:

答案 0 :(得分:1)

另外,它首先失败的原因是因为你需要做一个跨域帖子。为了通过jQuery正确执行此操作,您需要使用以下选项:

        dataType: 'jsonp',
        cache: false,

答案 1 :(得分:1)

我转载了你的错误。在Chrome中,如果单击inspect元素然后选择Console选项卡,则可以看到错误消息:

  

XMLHttpRequest无法加载file:/// D:/ DeleteME / pyMail / some%20url。   交叉源请求仅支持协议方案:http,   数据,chrome-extension,https,chrome-extension-resource。

如前所述,如果要发布到另一个域,则需要将数据类型指定为jsonp。

示例:

$.ajax({ type: "POST", contentType:"application/jsonp; charset=utf-8", dataType: "application/json", url:"http://www.google.com",

    data :{familyVoterId:'0',boothId:'0',voterId:'59136604',previousEnrollmentNumber:'29422415',casteId:'62',street:'hamlet',voterRelationId:'8',cadrePrevYear:'',relativeName:'DESSAPPA',previousRollesList:[{fromDateStr:'2012-10-24',cadreRoleId:'1',cadreCommitteeId:'1',cadreCommitteeLevelId:'1',toDateStr:"2013-10-24"}],partyMemberSinceStr:"2001-10-24",uniqueKey:"10d11073-a8b1-4764-bf2f-e2c0a269e9cd",nameType:"Voter",panchayatId:"0",age:28,surveyTimeStr:"2014-10-24 15:05:15",gender:"M",relationTypeId:8,bloodGroupId:3,dobStr:"1968-02-02",nomineeGender:"1",photoType:"VOTER",voterCardNumber:"UXN0862806",longititude:"78.413",aadheerNo:"987654321",candidateAadherNo:"12345678910",educationId:"2",nomineeAge:"21",previousParicaptedElectionsList:[{"electionTypeId":17,candidateId:"30126",constituencyId:"461"}],voterName:"RAMAKRISHNA",occupationId:"1",houseNo:"3-60",cadreFamilyDetails:[],createdUserId:"152",refNo:"TR-T-9999-12985",constituencyId:"282",latitude:"17.4278",nomineeName:"nominee",mobileNumber:"9999999999"},
    dataType: 'jsonp',
    success: function(data)
    {

        alert("success");
    },

    error: function(msg)
    {
        alert(msg.message);
    }
});

答案 2 :(得分:0)

消息不是错误回调函数中XHR对象msg的属性。这就是它在警报框中显示未定义的原因。您可以尝试提醒msg.statusText并查看状态文本。

请查看文档http://api.jquery.com/jquery.ajax/ (jqXHR对象部分)