无法在我的asp.net MVC 4应用程序中调用Json Web服务 [英] Fail to call a Json web service inside my asp.net mvc 4 application
问题描述
I want to call the following webservice as mentioend on this link http://dev.joget.org/community/display/KB/JSON+API#JSONAPI-web%2Fjson%2Fworkflow%2Fprocess%2Flist
所以在我的asp.net mvc视图中,我写了以下内容:-
So inside my asp.net mvc view i wrote the folloiwng:-
<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 
;直接在浏览器的地址栏上,将显示所有过程.那么可能出什么问题了?
But when i run the above web page no processes will be displayed under the <h1>The Processes are </h1>
, while if i type the following http://localhost:8080/jw/web/json/workflow/process/list?j_username=kermit&hash=9449B5ABCFA9AFDA36B801351ED3DF66&loginAs=admin 
; directly on the address-bar of my browser then all the processes will be displayed. so what might be going wrong ?
BR
-:::: UPDATED ::::- 我已更新我的JAVASCRIPT,如下所示:-
-::::UPDATED::::- i HAVE UPDATED MY JAVASCRIPT TO BE AS FOLLOW:-
$(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服务调用的结果返回为
but the result of the web service call is returned as
undefined: $ undefined
而不是像这样的东西:-
instead of being something such as:-
{"activityId":"","processId":"289_process1"}
那是什么导致我的代码无法显示正确的数据呢?
So what is the problem that is preventing my code from displaying the right data ?
推荐答案
尝试在查询字符串的末尾将JSONP callback
参数指定为callback=?
,如文档所述:
Try specifying the JSONP callback
parameter as callback=?
at the end of your query string as explained in the documentation:
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');
});
});
更新:
UPDATE:
最后显示了JSON之后,产品列表包含在data
属性中,因此您需要调整$.each
并在此属性上进行操作,而不是直接在结果上进行操作:
After showing your JSON (at last) the list of products is contained within a data
property, so you need to adapt your $.each
and operate on this property and not directly on the result:
{
"data": [
{
"packageName": "CRM",
"packageId": "crm"
},
{
"packageName": "EngineersAssociation",
"packageId": "EngineersAssociation"
},
{
"packageName": "LeaveApp",
"packageId": "leaveApp"
},
{
"packageName": "Newtest",
"packageId": "Newtest"
},
{
"packageName": "TestApp",
"packageId": "testapp"
},
{
"packageName": "test54",
"packageId": "test5"
}
]
}
因此,请修改您的代码:
So adapt your code:
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');
});
});
这篇关于无法在我的asp.net MVC 4应用程序中调用Json Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!