0

我想在此链接http://dev.joget.org/community/display/KB/JSON+API#JSONAPI-web%2Fjson%2Fworkflow%2Fprocess%2Flist上将以下 Web 服务称为 mentioend

所以在我的 asp.net mvc 视图中,我写了以下内容:-

<script type="text/javascript">
            $(document).ready(function () {
                // Send an AJAX request
                $.getJSON("http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin",
                function (data) {
                    // On success, 'data' contains a list of products.
                    $.each(data, function (key, val) {

                        // Format the text to display.
                        var str = val.id + ': $' + val.packageName;

                        // Add a list item for the product.
                        $('<li/>', { text: str })
                        .appendTo($('#products'));
                    });
                });
            });
</script>
<h1>The Processes are</h1>
<ul id="products"/>

但是当我运行上面的网页时,不会在下面显示任何进程<h1>The Processes are </h1>,而如果我输入以下内容http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin&nbsp;直接在我浏览器的地址栏上,然后将显示所有进程。那么可能出了什么问题?

BR

-::::UPDATED::::- 我已经更新了我的 JAVASCRIPT 如下:-

$(document).ready(function(){

           $.ajax({
               type: "GET",
               url: "http://localhost:8080/jw/web/json/workflow/package/list?loginAs=admin",

               dataType: "JSONP",
               contentType: "application/json; charset=utf-8",
               success: function (data) {
                   $.each(data, function (key, val) {

                       // Format the text to display.
                       var str = val.packageId + ': $ ' + val.packageName;

                       // Add a list item for the product.
                       $('<li/>', { text: str })
                       .appendTo($('#products'));

                   });
               }});
           });

但 Web 服务调用的结果返回为

undefined: $ undefined

而不是像这样的东西:-

{"activityId":"","processId":"289_process1"}

那么阻止我的代码显示正确数据的问题是什么?

4

1 回答 1

1

尝试按照文档中的说明将 JSONPcallback参数指定为callback=?查询字符串的末尾:

var url = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin&callback=?';
$.getJSON(url, function (data) {
    // On success, 'data' contains a list of products.
    $.each(data, function (key, val) {
        // Format the text to display.
        var str = val.id + ': $' + val.packageName;
        // Add a list item for the product.
        $('<li/>', { text: str }).appendTo('#products');
    });
});

更新:

在显示您的 JSON(最后)后,产品列表包含在一个data属性中,因此您需要调整您的$.each并在此属性上操作,而不是直接在结果上操作:

{
    "data": [
        {
            "packageName": "CRM",
            "packageI‌​d": "crm"
        },
        {
            "packageName": "EngineersAssociation",
            "packageId": "EngineersAssociation"
        },
        {
            "packageName": "LeaveApp",
            "packageId": "leaveApp"
        },
        {
            "packageName": "Newtest",
            "packageId": "Newtest"
        },
        {
            "p‌​ackageName": "TestApp",
            "packageId": "testapp"
        },
        {
            "packageName": "test54",
            "packageId": "test5"
        }
    ]
}

所以调整你的代码:

var url = 'http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin&callback=?';
$.getJSON(url, function (result) {
    // On success, 'result.data' contains a list of products.
    $.each(result.data, function (key, val) {
        // Format the text to display.
        var str = val.id + ': $' + val.packageName;
        // Add a list item for the product.
        $('<li/>', { text: str }).appendTo('#products');
    });
});
于 2012-10-09T10:42:42.807 回答