【问题标题】:ajax call json to webservice in vbajax在vb中调用json到webservice
【发布时间】:2015-04-28 14:02:20
【问题描述】:

当我使 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' }

数据类型:“文本” 控制台.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 中的 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)吗?

标签: ajax json vb.net cordova


【解决方案1】:

返回 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' }

【讨论】:

  • 这确实是一个问题..我自己解决了这个问题..我会发布答案..无论如何感谢您的快速响应!
  • 如果我的回答能帮助您解决问题,您可以投票赞成。
【解决方案2】:

您的方法必须共享,除非您这样做,否则您将无法调用这些方法:

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

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

【讨论】:

  • 不,这不是问题......我用一些叫做谷歌的东西修复了它,3页后我找到了我的解决方案。但我非常感谢您的反应!
【解决方案3】:

谢谢

我找到了我的解决方案,但无论如何我都会发布它。如果有人偶然发现同样的问题。 =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 调用和来自我的网络服务的响应

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);
                    }
                }
            });

还有我的网络服务..

网络服务

<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

如果有人有更好的解决方案,我仍然会标记它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多