当我使json内联它确实有效。但是,当我使用网络服务时,它不起作用。我的想法是,我打电话给json从服务器"抓取数据。 我使用phonegap,我错过了什么吗?随时留言。
**编辑
数据类型:" JSON",
console.log =
[phonegap] [console.log] { readyState: 4,
responseText: '{"rapporten":[{"image":"img/house.jpg","straatnaam":"Dornaardst
raat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img/house2.
jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercode":"9154
374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "postcode"
:"1257 HS","dossiercode":"9652063174"}]}{"d":null}',
status: 200,
statusText: 'OK' }
数据类型:" HTML"
console.log =
[phonegap] [console.log] { readyState: 4,
responseText: '{"rapporten":[{"image":"img/house.jpg","straatnaam":"Dornaardst
raat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img/house2.
jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercode":"9154
374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "postcode"
:"1257 HS","dossiercode":"9652063174"}]}{"d":null}',
status: 200,
statusText: 'OK' }
数据类型:"文本" console.log =
[phonegap] [console.log] {"rapporten":[{"image":"img/house.jpg","straatnaam":"Do
rnaardstraat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img
/house2.jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercod
e":"9154374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "p
ostcode":"1257 HS","dossiercode":"9652063174"}]}{"d":null}
HTML
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json",
data: "",
url: "http://localhost:1460/wsApp.asmx/TestingJsonCall",
dataType: "json",
error: function (data) {
console.log("error underneath this:")
console.log(data);
},
success: function (data) {
console.log(data)
try {
var ob = jQuery.parseJSON(data); //this does not work
//i stringify because of the jquery.parseJSON
data = JSON.stringify(data)
//var ob = jQuery.parseJSON('{"rapporten":[{"image":"img/house.jpg","straatnaam":"Dornaardstraat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img/house2.jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercode":"9154374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "postcode":"1257 HS","dossiercode":"9652063174"}]}');
console.log(ob);
var A = 1;
var html ='';
var length = ob.rapporten.length;
for (var i = 0; i < length; i++ & A++) {
html += '<div class="rapportItem padding3precent">';
html += '<span id="rapport' + A + '"></span>';
html += '<div class="rapportImage">';
html += '<img id="currentReportsImg' + A + '" src=""/>';
html += '</div>';
html += '<div class="rapportInformation">';
html += '<h3 id="strNm' + A + '"></h3>';
html += '<h3 id="post' + A + '"></h3>';
html += '<p id="docNum' + A + '"></hp>'
html += '</div>';
html += '</div>';
}
$('#content').html(html)
var B = 1;
for (var i = 0; i < length; i++ & B++) {
$("#currentReportsImg" + B).attr("src", ob.rapporten[i].image)
$("#strNm" + B).html(ob.rapporten[i].straatnaam)
$("#post" + B).html(ob.rapporten[i].postcode)
$("#docNum" + B).html(ob.rapporten[i].dossiercode)
}
} catch (e) {
console.log("FOUT JSON PARSE: " + e);
}
},
});
});
visual basic中的webservice
<WebMethod> _
Public Sub testingJsonCall()
Dim test = "{" + Chr(34) + "rapporten" + Chr(34) + ":[{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house.jpg" + Chr(34) + "," + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "Dornaardstraat 14" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "4901 BN" + Chr(34) + "," + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "8543529681" + Chr(34) + "},{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house2.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "andersstraat 113" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "4901 AA" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "9154374529" + Chr(34) + "},{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house3.jpg" + Chr(34) + "," + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "vuurtorenstraat 21" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "1257 HS" + Chr(34) + "," + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "9652063174" + Chr(34) + "}]}"
HttpContext.Current.Response.Write(test)
End Sub
答案 0 :(得分:0)
返回JSON时,你不应该围绕你的对象。 所以:
{ readyState: 4,
responseText: '{"rapporten":[{"image":"img/house.jpg","straatnaam":"Dornaardst
raat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img/house2.
jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercode":"9154
374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "postcode"
:"1257 HS","dossiercode":"9652063174"}]}{"d":null}',
status: 200,
statusText: 'OK' }
应该是:
{ readyState: 4,
responseText: {"rapporten":[{"image":"img/house.jpg","straatnaam":"Dornaardst
raat 14", "postcode":"4901 BN","dossiercode":"8543529681"},{"image":"img/house2.
jpg", "straatnaam":"andersstraat 113", "postcode":"4901 AA", "dossiercode":"9154
374529"},{"image":"img/house3.jpg","straatnaam":"vuurtorenstraat 21", "postcode"
:"1257 HS","dossiercode":"9652063174"}]}{"d":null},
status: 200,
statusText: 'OK' }
答案 1 :(得分:0)
除非您执行此操作,否则必须共享您的方法,否则您将无法调用方法:
collectionViewContentSize()
答案 2 :(得分:0)
谢谢
我找到了我的解决方案,但无论如何我都会发布它。如果有人偶然发现同样的问题,那就麻烦了。 =D
<强> HTML 强>
<link href="css/loads.css" rel="stylesheet" />
<div id="content">
Sorry, there is a connection error...<br/>
Please wait a second.<br />
else , try to restart the program.
</div>
但问题是我的ajax调用和来自我的webservice的响应
ajax电话
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8;",
url: "http://localhost:1460/wsApp.asmx/TestingJsonCall1",
data: "{}",
datatype: "JSON",
error: function (data) { console.log('ERR'); },
dataFilter: function (data) {
var msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
success: function (msg) {
console.log(msg);
var A = 1;
var html = '';
var length = msg.rapporten.length;
for (var i = 0; i < length; i++ & A++) {
html += '<div class="rapportItem padding3precent">';
html += '<span id="rapport' + A + '"></span>';
html += '<div class="rapportImage">';
html += '<img id="currentReportsImg' + A + '" src=""/>';
html += '</div>';
html += '<div class="rapportInformation">';
html += '<h3 id="strNm' + A + '"></h3>';
html += '<h3 id="post' + A + '"></h3>';
html += '<p id="docNum' + A + '"></hp>';
html += '</div>';
html += '</div>';
}
$('#content').html(html)
var B = 1;
for (var i = 0; i < length; i++ & B++) {
$("#currentReportsImg" + B).attr("src", msg.rapporten[i].image);
$("#strNm" + B).html(msg.rapporten[i].straatnaam);
$("#post" + B).html(msg.rapporten[i].postcode);
$("#docNum" + B).html(msg.rapporten[i].dossiercode);
}
}
});
和我的网络服务..
<强> web服务强>
<WebMethod> _
Public Function reports() As String
Dim currentReports As String = "{" + Chr(34) + "rapporten" + Chr(34) + ":[" + _
"{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house.jpg" + Chr(34) + "," + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "Dornaardstraat 14" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "4901 BN" + Chr(34) + "," + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "8543529681" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house2.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "andersstraat 113" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "8246 AA" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "9154374529" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house3.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "Torenstraat 72a" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "6735 GH" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "5727257342" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "andersstraat 21" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "6832 EN" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "4836136116" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house3.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "ietsstraat 235" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "8145 PH" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "8643285938" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house2.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "dummystreet 3" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "5721 QT" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "1379428192" + Chr(34) + "}" + _
",{" + Chr(34) + "image" + Chr(34) + ":" + Chr(34) + "img/house.jpg" + Chr(34) + ", " + Chr(34) + "straatnaam" + Chr(34) + ":" + Chr(34) + "iemandstraat 11" + Chr(34) + ", " + Chr(34) + "postcode" + Chr(34) + ":" + Chr(34) + "4782 TB" + Chr(34) + ", " + Chr(34) + "dossiercode" + Chr(34) + ":" + Chr(34) + "7254861307" + Chr(34) + "}" + _
"]}"
Return currentReports
End Function
如果有人有更好的解决方案,我仍然会标记它。