當我讓內聯json它確實工作。但是當我使用web服務時,它不起作用。想法是我調用json從「服務器」中獲取數據。 我使用phonegap工作,我錯過了什麼?隨時留言。ajax調用json到webservice在vb
**編輯
數據類型: 「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);
}
},
});
});
web服務在視覺基本
<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
你試過'JSON.parse(data)'嗎? –