[英]How to call .asmx web service hosted on Remote server from plain html using ajax
I've created a simple web service using asp.net and hosted on my machine's IIS server. 我使用asp.net创建了一个简单的Web服务,并将其托管在计算机的IIS服务器上。 I am trying to call this web service from plain html page without using Asp.net. 我试图不使用Asp.net从纯HTML页面调用此Web服务。 The problem is that I am not getting response. 问题是我没有得到回应。 Here is my code below : 这是我的代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calling Classic Web Services with jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a#SayHello").click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: '{firstName:10,lastName:15}',
// url: 'Service.asmx/SayHello',
url: 'http://192.168.1.20/MyService/Service.asmx/SayHello',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(response) {
alert(response.d); //getting the Response from JSON
},
failure: function(msg) {
alert(msg);
}
});
});
});
</script>
</head>
<body>
<input id="name" /><a id="SayHello" href="#">Greetings!</a>
</body>
</html>
Can anybody please help me where I am going wrong in this code..?? 有人可以帮我解决这段代码中的错误吗? If I run this in ASP.Net environment, it works perfectly. 如果我在ASP.Net环境中运行此程序,它将运行完美。 But if I host this, then it doesn't work. 但是,如果我主持此活动,那么它将无法正常工作。 Please help me...!! 请帮我...!!
Are you sure the web service is responding in JSON format? 您确定Web服务以JSON格式响应吗? It usually answers in XML. 它通常以XML回答。 Also your <script>
block should go in the <body>
and not in the <head>
. 同样,您的<script>
块应放在<body>
而不是<head>
。
如果您已将Web服务托管在同一IIS上,则将192.168.1.20更改为localhost,然后尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.