[英]Unable to retrieve JSON data from url in html page using javascript
我创建了一个示例 WCF 服务,用于从数据库检索数据并将其显示为浏览器中的 JSON 数据。此任务已成功完成。
url接收到的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>
看看你的 url 是一个字符串而不是变量名!
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;
});
要使用从 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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.