我已经创建了一个示例WCF服务,用于从数据库中检索数据并在浏览器中将其显示为JSON数据。此任务已成功完成。
网址收到的JSON数据是:
{"shipmentDetails":[{"Name":"AAA","Number":"123"},{"Name":"BBB","Number":"321"}]}
创建上述服务后,我的任务是在单击按钮时在html页面中查看相同的数据。我使用javascript / jQuery从URL接收数据,但是当我点击按钮时没有执行任何操作。
以下是带有javascript的HTML:
<html>
<head>
<title>JSON</title>
<script language="javascript" type="text/javascript" src="http://code.jquery.com /jquery-1.5.min.js">
</script>
<script language="javascript" type="text/javascript" >
$(document).ready(function(){
$("testButton").click(function() {
$.ajax({
type: "GET",
dataType: 'jsonp',
contentType: "application/jsonp; charset=utf-8",
url: "http://localhost:4148/EIS.svc/getShipment/arun/arun",
success: function (data) {
obj = eval('(' + data + ')');
alert(obj);
var innerHtml = "";
document.getElementById('test').innerHTML=obj;
//'test' is ID of <label1>
document.getElementById('testlab').innerHTML=obj.shipmentDetails[0].Name;
//'testlab' is ID of <label2>
$("#test").html(innerHtml);
alert("JSON DATA");
alert(obj.shipmentDetails[0].Number);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error while retrieval – " + XMLHttpRequest.responseText+":"+textStatus+":"+errorThrown);
}
});
});
});
</head>
<body>
<input type="button" id="testButton" value="GET JSON"/>
<label id="test"></label>
<label id="testlab"></label>
</body>
</html>
答案 0 :(得分:2)
看一下你的网址是一个字符串而不是变量名!
type: 'GET',
url: serviceURL /* instead of 'serviceURL'*/,
这是完整的代码
$('#testButton').click(function() {
//'testButton' is ID of button input type in html
alert("Button clicked");
var serviceURL="http://localhost:4148/EIS.svc/getShipment/json/data";
$.ajax({
type: 'GET',
url: serviceURL,
data:$('#serviceURL').serialize(),
processData: false,
dataType: 'jsonp',
contentType: "application/jsonp; charset=utf-8",
//If the call succeeds
success:function (data) {
alert(data);
},
//If the call fails
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error while retrieval – " + XMLHttpRequest.responseText+":"+textStatus+":"+errorThrown);
}
});
return false;
});
答案 1 :(得分:0)
对于使用从URL返回的JSON数据,必须将其包装在回调函数中,如下所示:
包装数据之前的URL是:
[http://localhost:4148/EIS.svc/getShipment/json/data]
在回调函数中包装数据后的URL是:
[http://localhost:4148/EIS.svc/getShipment/json/data?callback=jsonpexp]
在URL中进行上述更改后,浏览器中返回的数据如下所示:
jsonpexp({"shipmentDetails":[{"Name":"AAA","Number":"123"},{"Name":"BBB","Number":"321"}]});
然后它需要在javascript中进行更改,如下所示:
$('#testButton').click(function() {
//'testButton' is ID of button input type in html
$.ajax({
type: "GET",
dataType: 'jsonp',
contentType: "application/jsonp; charset=utf-8",
url: "http://localhost:4148/EIS.svc/getShipment/arun/arun?callback=jsonpexp",
jsonpCallback: 'jsonpexp',
success: function (data) {
var innerHtml = "";
document.getElementById('test').innerHTML=data.shipmentDetails[0].Name;;
//'test' is ID of <label1>
document.getElementById('testlab').innerHTML=data.shipmentDetails[0].Number;
//'testlab' is ID of <label2>
document.getElementById('test2').innerHTML=data.shipmentDetails[1].Name;;
//'test2' is ID of <label3>
document.getElementById('testlab2').innerHTML=data.shipmentDetails[1].Number;
//'testlab2' is ID of <label4>
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error while retrieval – " + XMLHttpRequest.responseText+":"+textStatus+":"+errorThrown);
}
});
return false;
});