2015-04-28 43 views
0

當我讓內聯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 
+0

你試過'JSON.parse(data)'嗎? –

回答

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' } 
+0

這確實是一個問題..我自己解決了這個問題..我會發布答案..謝謝無論如何快速響應! – santinobonora

+0

如果它導致你解決你的問題,你可以upvote我的答案。 –

0

你的方法,已被共享,除非你這樣做,你將無法調用的方法:

<WebMethod> _ 
    Public Shared Sub testingJsonCall() 
    Dim test = "something" 

    HttpContext.Current.Response.Write(test) 
End Sub 
+1

不,這不是問題...我用谷歌和3頁後,我找到了我的解決方案。但我非常感謝反應! – santinobonora

0

THANK YOU

我找到了我的解決方案,但無論如何我會發布它..馬貝如果有人在同一個問題上絆倒。 =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調用和響應從我的web

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服務..

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 

如果有人有更好的解決辦法,我還是會對它進行標記。