我注意到产品API中的错误似乎是什么。问题如下: - 1.如果返回的JSON包含多个进程(在我的情况下是两个进程),则JSON结果如下: -
{"total":2,"data":[{"id":"leaveApp#2#leaveApplicationProcess","packageName":"Leave App","name":"Leave Application Process","packageId":"leaveApp","label":"Leave Application Process ver 2","version":"2"},{"id":"leaveApp#2#process1","packageName":"Leave App","name":"Leave Application Process2","packageId":"leaveApp","label":"Leave Application Process2 ver 2","version":"2"}]}
我可以使用以下javaScript显示这两个进程: -
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin&packageId=leaveApp",
dataType: "JSONP",
success: function (result) {
$.each(result.data, function (key, val) {
var str = val.name + ' | Version ' + val.version;
$('<li/>', { text: str })
.appendTo($('#products'));
}); }
}); });
如果packageid只包含一个进程,那么上面的Java脚本将为JSON返回"undefined"
,因为我认为因为没有数据属性,返回的JSON格式如下: -
{"total":1,"data":{"id":"crm#1#process1","packageName":"CRM","name":"Proposal Approval Process","packageId":"crm","label":"Proposal Approval Process ver 1","version":"1"}}.
所以任何人都可以帮助我如何使我的javascruipt工作,无论进程数量多少或这是API本身的问题?
最好的问候
更新
I modify my JavaScript to be as follow:-
$.ajax({
type: "GET",
// url: "http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin&packageId=" + 'name',
url: fullurl,
dataType: "JSONP",
// contentType: "application/json; charset=utf-8",
success: function (result) {
var arr = ( (typeof(result.data) == 'object') &&
(result.data.constructor.name == 'Array') )
? result.data
: [result.data];
$.each(arr, function (key, val) {
// Format the text to display.
// var str = val.packageName + ' | ' + val.packageId;
var str = val.name + ' | Version ' + val.version;
// Add a list item for the product.
$('<li/>', { text: str })
.appendTo($('#products'));
});
}
});
但是目前如果有多个JSON对象,它将返回“undefined”,而如果只有一个JSOn对象,那么它将正常工作......所以我原来的问题现在是副Versa。
答案 0 :(得分:0)
这是因为JSON数据的格式不一致。要修复它,你总是可以使data
作为数组返回,无论是否有多个进程(数组的元素)。
或者,试试这个:
var arr = ( (typeof(result.data) == 'object') &&
(result.data.constructor.name == 'Array') )
? result.data
: [result.data];
$.each(arr, function (key, val) {
// ... rest goes here
});
编辑:修复了object
中的拼写错误。