我一直在爲此抨擊近2天。我已經閱讀了數百篇博文,SO帖子和MSDN文章,而且我還沒有得到我的WCF服務的表現!WCF帶有JSON輸出行爲異常的Web服務
一些背景資料: 我有兩個項目:
- 1 C#Web應用程序 - 在WCF數據將通過JQuery的消耗。我寫了一個JQuery代理,但我不確定它是否還能工作,因爲我的WCF服務似乎沒有響應!
- 1 C#WCF服務 - 目前,我不希望任何事情比針對某些數據的驗證服務更復雜。我想使用JQuery來檢查這些服務來驗證表單上的用戶輸入(例如有效的電子郵件)。
的服務 - 目前,我只是試圖讓IsEmailValid工作:
namespace AtomicService
{
[ServiceContract]
public interface IValidation
{
[OperationContract, WebInvoke(
Method="POST",
BodyStyle= WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string IsEmailValid(string email);
[OperationContract]
bool DoesClientExist(string client);
[OperationContract]
bool IsPasswordOk(string password);
[OperationContract]
bool IsPostcodeValid(string postcode, string isoalpha2);
[OperationContract]
bool IsTelephoneValid(string telephone, string isoalpha2);
}
}
我的實現:
namespace AtomicService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Validation : IValidation
{
public string IsEmailValid(string email)
{
return string.Format("Response:{0}", AtomicCore.Validation.CheckEmail(email).ToString());
}
//others removed for brevity
}
}
當我測試使用WCFTestClient我的web服務,一切都很好:
使用Fiddler檢查返回400錯誤請求:
此外,當我嘗試用瀏覽器訪問該服務,絕對沒有發生 - 只是一個空白頁。
當我用我的JQuery代理:
/// <reference path="~/System/jquery.js" />
ServiceProxy = function() //constructor for the proxy
{
this._baseURL = "http://127.0.0.1:88/Validation.svc/";
};
ServiceProxy.prototype =
{
// getArticles: function (success, error) {
// this._doAjax("GetArticles", null, success, error);
// },
isEmailValid: function (email, success, error) {
var data = { email: email };
this._doAjax("IsEmailValid", data, success, error)
},
_defaultErrorHandler: function (xhr, status, error) {
alert(xhr.statusText + ' ' + error);
},
_doAjax: function (method, data, fnSuccess, fnError) {
if (!data) data = {};
if (!fnError) fnError = this._defaultErrorHandler;
$.ajax({
type: "GET",
url: this._baseURL + method,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: fnSuccess,
error: fnError,
dataFilter: function (data) {
var response;
if (typeof (JSON) !== "undefined" && typeof (JSON.parse) === "function")
response = JSON.parse(data);
else
response = eval("(" + data + ")");
if (response.hasOwnProperty("d"))
return response.d;
else
return response;
}
});
}
};
我得到一個錯誤 - 但不知道是什麼錯誤。我再也看不到樹木了!
我完全困惑? WCF看起來恐怕比舊的.NET ASMX服務方法更復雜,但我想嘗試學習 - 迄今爲止這是痛苦的!
一如既往的幫助,讚賞。
感謝地塊。我將Fiddler請求更改爲POST,現在正在接收'HTTP 411請求必須分塊或有內容長度'錯誤。任何直接的線索? +1作爲非常快速的響應;) – dooburt 2011-03-08 15:39:00
通常,請求應該有一個名爲Content-Length的頭部,它表示請求體的大小(以字節爲單位)。儘管您通常可以逃避零測試目的。 (請參閱:http://groups.google。com/group/httpfiddler/browse_thread/thread/420f97ac7982e10c/cc9b10fd914ca45a?show_docid = cc9b10fd914ca45a) - 我假設鏈接很好,標題聽起來很正確,在工作時沒有任何訪問:( – Massif 2011-03-08 15:42:26
感謝地塊,這肯定會讓我走上正確的道路。我現在有其他錯誤,但我會嘗試找出它們,並在必要時發佈另一個SO問題。非常感謝您的意見。 – dooburt 2011-03-08 15:59:20