我是asp.net和网络服务的网络用户。我正在开发调用asp.net web服务的HTML5应用程序。我已经在IIS7上发布了我的asp.net web服务,它工作得很好,但是当我通过外部HTML5 JQuery调用web服务时,它给了我未定义的错误。
以下是我的web服务代码:
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Byeeee";
}
}我的Jquery代码是:
// JavaScript Document
$(document).ready(function() {
$.ajax({
type: "POST",
url: "http://localhost/mywebserice/Service.asmx?op=HelloWorld",
Content-Type: 'application/x-www-form-urlencoded',
dataType: "xml",
data: '{}',
success: function(){
alert("Success");
},
error: function(){
alert("Error");
}
});
});我的HTML5代码是:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="newJS.js"></script>
</head>
<body>
</body>
</html>有谁能帮我解决这个问题吗?
发布于 2013-12-16 18:48:52
您的jQuery ajax设置属性错误。
首先,没有Content-Type这样的属性。使用contentType。
其次,您指定了错误的url结构。ajax url的结构应该如下所示:
domain/ServiceName.asmx/MethodName?anyParamters=value如果调用webservice的页面和webservice属于同一个域,也可以指定相对url。
~/ServiceName.asmx/MethodName?anyParamters=value将ajax函数更改为:
$.ajax({
type: "POST",
url: "http://localhost/mywebserice/Service.asmx/HelloWorld",
contentType: 'application/x-www-form-urlencoded',
dataType: "xml",
data: {},
success: function (msg) {
alert($(msg).text());
//console.log($(msg).text());
},
error: function(xhr, status, error){
console.log("Error");
}
});您可以阅读有关jQuery ajax here的所有可能属性。试一试
https://stackoverflow.com/questions/20608713
复制相似问题