2013-12-16 45 views
0

我網絡到asp.net和網絡服務。 我正在開發調用asp.net web服務的HTML 5應用程序。 我已經在IIS7上發佈了我的asp.net web服務,它工作正常,但是當我通過外部HTML 5 JQuery調用Web服務時,它給了我一個未定義的錯誤。從外部HTML頁面的asp.net 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> 

任何一個可以請幫我解決這個問題?

+0

使你的'在'網址'jQuery的Ajax到這個:'http:// localhost/mywebserice/Service.asmx/HelloWorld' –

+0

謝謝你的迴應。但是當我將url設置爲http://localhost/mywebserice/Service.asmx/HelloWorld時,它不顯示任何內容 –

回答

0

您的jQuery ajax設置屬性是錯誤的。

首先,沒有Content-Type這樣的屬性。使用contentType

其次,您指定了錯誤的url結構。阿賈克斯URL的結構應該是這樣的:

domain/ServiceName.asmx/MethodName?anyParamters=value 

你也可以指定相對URL,如果頁面要從中調用Web服務和Web服務都屬於同一個域I,E,

~/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的可能的屬性。給它一個去

相關問題