jquery - Unable to retrieve JSON data from url in html page using javascript -
i have created sample wcf service retrieving data database , display json data in browser.this task completed successfully.
the json data received url is:
{"shipmentdetails":[{"name":"aaa","number":"123"},{"name":"bbb","number":"321"}]}
after creating above service task view same data in html page when button clicked. using javascript/jquery receiving data url, when clicked button no action performed.
following html javascript:
<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' id of <label1> document.getelementbyid('testlab').innerhtml=obj.shipmentdetails[0].name; //'testlab' 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>
have url string instead of variable name!
type: 'get', url: serviceurl /* instead of 'serviceurl'*/,
here complete code
$('#testbutton').click(function() { //'testbutton' 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 call succeeds success:function (data) { alert(data); }, //if call fails error: function (xmlhttprequest, textstatus, errorthrown) { alert("error while retrieval – " + xmlhttprequest.responsetext+":"+textstatus+":"+errorthrown); } }); return false; });
Comments
Post a Comment