問題是如何從JQuery訪問非aspx服務。從JQuery調用WCF服務不起作用
我曾嘗試:
最簡單的情況:
合同:
using System.ServiceModel;
using System.ServiceModel.Web;
namespace TheService
{
[ServiceContract]
public interface ITheService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string Get();
}
}
的服務:使用系統 ; using System.ServiceModel.Web;
namespace TheService
{
public class TheService : ITheService
{
public string Get()
{
return "Hello, world!";
}
}
}
託管服務:
class Program
{
static void Main(string[] args)
{
Uri httpUrl = new Uri("http://localhost:22334/TheService");
ServiceHost host = new ServiceHost(typeof(TheService), httpUrl);
ServiceMetadataBehavior serviceMetaDataBehaviour = new ServiceMetadataBehavior
{
HttpGetEnabled = true,
MetadataExporter = {PolicyVersion = PolicyVersion.Policy15}
};
host.Description.Behaviors.Add(serviceMetaDataBehaviour);
host.AddServiceEndpoint(typeof(ITheService), new WebHttpBinding(), "");
host.Open();
Console.ReadLine();
}
}
如果我嘗試用wcftestclient訪問服務,我可以訪問數據。
現在我想通過jQuery
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$.getJSON("http://localhost:22334/TheService/Get",
function(data){
alert("Data Loaded: " + data);
});
});
</script>
</head>
<body>
</body>
</html>
我認爲應該發生的事情是,我得到一個Hello World回訪問它。但事實並非如此。
我想:
$.ajax({
type: "GET",
url: "http://localhost:22334/TheService/Get",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg);
},
error: function(err) {
alert(err.toString());
if (err.status == 200) {
ParseResult(err);
}
else { alert('Error:' + err.responseText + ' Status: ' + err.status); }
}
});
和所有我得到的是兩個警報「[對象]對象」和「錯誤:狀態:0」
所以在這種情況下,它似乎運行到一些錯誤,但在哪?
我從來沒有使用過jQuery,所以有什麼地方可以得到一些更有用的錯誤信息?
我需要在wcf服務中定義其他任何東西嗎?
最後,兩個項目不應該在相同的應用程序。應該在客戶端使用JQuery,並在集中式服務器上運行wcf服務。
如果我改變
ServiceHost host = new ServiceHost(typeof(TheService), httpUrl);
到
WebServiceHost host = new WebServiceHost(typeof(ServiceCalculator), httpUrl);
我可以看到,到達該WCF服務器調試點(文本返回) - 但仍兩種錯誤對話框是顯示。
(在瀏覽器中增加了localhost:22334/TheService/Get返回「Hello World」 - 因此我認爲這是JQuery/Ajax的問題,還是?)
更新:
相應的答覆,並http://pranayamr.blogspot.de/2011/06/calling-cross-domain-wcf-service-using.html我增加了以下內容:
設置調試爲true:
ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
if (debug == null)
{
host.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
}
else
{
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
改變行爲,以ASP的兼容性
for (int i = 0; i < host.Description.Behaviors.Count; i++)
{
if (host.Description.Behaviors[i] is AspNetCompatibilityRequirementsAttribute)
{
host.Description.Behaviors.RemoveAt(i);
break;
}
}
host.Description.Behaviors.Add(new AspNetCompatibilityRequirementsAttribute { RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed });
允許crossdomainscriptaccess
host.Description.Behaviors.Add(serviceMetaDataBehaviour);
WebHttpBinding binding = new WebHttpBinding {CrossDomainScriptAccessEnabled = true};
host.AddServiceEndpoint(typeof(IServiceCalculator), binding, "");
,並試圖設置JSONP
$.ajax({
type: "GET",
url: "http://localhost:22334/TheService",
method: "Get",
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
success: function(msg) {
alert(msg);
},
error: function(err) {
alert(err.toString());
if (err.status == 200) {
ParseResult(err);
}
else { alert('Error:' + err.responseText + ' Status: ' + err.status); }
}
});
現在看來,以某種方式工作。 (如果出現問題,仍然沒有真正的錯誤信息...)(並且似乎也不需要asp部分)
我不這麼認爲,似乎所有的測試都是在'localhost'上完成的。 –
端口,協議和域必須相同。 – meilke
你是對的;我認爲你的回答可能比我自己的回答更有意義。 –