admin管理员组文章数量:1394544
I'm using the following code to call the webservice by using jQuery ajax. But It doesn't work? The webservice return the value in JSON format. How can I access the webservice through this code?
<html>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function () {
$('input[id^="button"]').click(function () {
alert('You have clicked ' + $(this).val());
$.ajax({
type: 'Get',
url: 'http://localhost:56789/xxx/Handler.ashx?key=yyy ',
success: function (data) {
alert(data);
}
});
})
})
</script>
<body>
<div id="Sample_Div">
<input type="button" id="button1" value="button1" />
</div>
</body>
</html>
I'm using the following code to call the webservice by using jQuery ajax. But It doesn't work? The webservice return the value in JSON format. How can I access the webservice through this code?
<html>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function () {
$('input[id^="button"]').click(function () {
alert('You have clicked ' + $(this).val());
$.ajax({
type: 'Get',
url: 'http://localhost:56789/xxx/Handler.ashx?key=yyy ',
success: function (data) {
alert(data);
}
});
})
})
</script>
<body>
<div id="Sample_Div">
<input type="button" id="button1" value="button1" />
</div>
</body>
</html>
Share
Improve this question
edited Apr 23, 2012 at 15:33
noob
9,2124 gold badges39 silver badges65 bronze badges
asked Apr 23, 2012 at 12:17
useruser
1893 gold badges8 silver badges18 bronze badges
9
- This works for me. What error do you get? – binarious Commented Apr 23, 2012 at 12:22
- Am not getting any error at the same time am not getting result – user Commented Apr 23, 2012 at 12:25
- 1 What does the Javascript Console say? – binarious Commented Apr 23, 2012 at 12:27
- Add type="text/javascript" to the script tag to get rid of that warning. Add the code for Handler.ashx. Is the request getting to your handler or is the handler not returning a valid response. – Kevin Junghans Commented Apr 23, 2012 at 12:56
- Handler give the response but I use the same URL in ajax i didn't get the value – user Commented Apr 23, 2012 at 13:03
2 Answers
Reset to default 2Maybe you can try this one.
$.ajax({
url: "../Services/Person.asmx/SavePersonById",
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
data: '{ID:"00123"}',
success: function (response) {
//do whatever your thingy..
}
});
Web Service Stuff:
[WebMethod]
public string SavePersonById(string ID)
{
//do some code here..
dbContext.Save(ID,"Firstname","Lastnmae");
return "Successfully Saved!";
}
You can try this:
$(document).ready(function () {
$('#button').click(function () {
$.ajax({
type: "POST",
url: "appWebservices/select.asmx/checkLogin",
data: "{ ID:'" + $(this).val()+ "'}",
contentType: "application/json;charset=utf-8",
datatype: "json"
});
});
});
Write Web services as follow:
[WebMethod]
public string checkLogin(string ID)
{
//Write your code here..
//return value
}
know moredetails
本文标签: javascripthow to call webservice using Ajax with jqueryStack Overflow
版权声明:本文标题:javascript - how to call webservice using Ajax with jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744093742a2589900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论