[英]Undefined Error on asp.net web-service call from external HTML page
我對asp.net和網絡服務有所了解。 我正在開發調用asp.net Web服務的HTML 5應用程序。 我已經在IIS7上發布了asp.net Web服務,並且運行良好,但是當我通過外部HTML 5 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>
誰能幫我解決這個問題?
您的jQuery ajax設置屬性錯誤。
首先,沒有諸如Content-Type
這樣的屬性。 使用contentType
。
其次,您指定的網址結構錯誤。 Ajax網址的結構應如下所示:
domain/ServiceName.asmx/MethodName?anyParamters=value
如果您要從中調用該Web服務的頁面與該Web服務屬於同一域(例如,
~/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的所有可能屬性。 搏一搏
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.