我正在开发一个win8应用程序,它应该使用托管在IIS上的WCF服务,但它不会... 注意:Web服务工作得很好。我在VS2012 WCF测试客户端进行测试呢?这里是我的代码:如何在Windows8 HTML5/JS应用程序中使用WCF服务?
IService.cs:
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
[ServiceContract]
public interface IService
{
[OperationContract]
string SitePredmeti();
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
List<Profesor> SiteProfesori();
}
我做了两个方法:一个返回XML一个回报JSON只是这样我就可以测试一个作品。无论WS返回json还是xml,我都可以在应用程序中使用。
我使用默认的SPLIT APP TEMPLATE作为win8中的html5/JS应用程序,无需任何更改。现在我只需要使用wcf服务,稍后我将处理数据模板。
链接到我的IIS托管WCF:
http://localhost/InformatorWS/Service.svc
这里是我试过到目前为止:
var baseURl = "http://localhost/InformatorWS/Service.svc/SitePredmeti/";
var url = baseURl+Number1.value+"/"+Number2.value;
WinJS.xhr({ url: url }).then(function (r) {
var result = JSON.parse(r.responseText);
ResultSpan.innerHTML = result; });
错误我来到这里是:
{"exception":null,"error":{},"promise":{"_oncancel":null,"_nextState":null,"_state":>{"name":"error","done":null,"then":null},"_listeners":null,"_value":{},"_isException":false,"_errorId":2},"id":2}
下一个我试图通过jQuery的ajax调用它:
$(document).ready(function() {
jQuery.support.cors = true;
//Calling WCF Service using jQuery ajax method
$.ajax({
type: "GET",
async: "false",
url: "http://localhost/InformatorWS/Service.svc/SitePredmeti",
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (result) {
//AjaxSucceeded(result);
alert('ok')
},
error: alert('ne ok')
});
});
,当我链接的jquery
<script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-1.4.3.js"></script>
我得到三个例外:
Unhandled exception at line 10, column 2 in ms-appx://66b782f9-8814-4b8e-a2e3-d6e73893690a/js/jquery-ui-1.8.9.custom.min.js 0x800a1391 - JavaScript runtime error: 'jQuery' is undefined
(其中两种 - 两个警报在成功和错误)
Unhandled exception at line 8, column 9 in ms-appx://66b782f9-8814-4b8e-a2e3-d6e73893690a/js/default.js 0x800a1391 - JavaScript runtime error: 'alert' is undefined
任何人有任何想法我做错了什么?或者任何人都知道使用IIS上托管的WCF服务的任何方式?
非常感谢您提前。
在jquery UI之前包含jquery,它应该修复你的jquery未定义问题。你可以尝试让web服务上网,看看你是否可以使用浏览器来使用它。 –