我寫了一個簡單的webmethod,我在客戶端調用以檢查數據庫中的文本更改是否存在值。它在本地很好地工作,但是當我將它移動到我們的開發環境時,它會在響應中返回頁面的整個HTML。我注意到的唯一情況是本地Response.Server是IIS7.5,但在我們的Dev服務器上是IIS6。
這裏是我的代碼:
服務器代碼
[ScriptMethod]
[System.Web.Services.WebMethod]
public static bool CheckInvoiceExists(string vendorNumber, string invoiceNumber)
{
try
{
return RequestEntry.CheckInvoiceExists(vendorNumber, invoiceNumber);
}
catch (Exception exp)
{
EventLogging.LogError("Error checking if invoice exists: " + exp.Message);
return false;
}
}
客戶端代碼
function CheckInvoiceExists() {
//var vendNo = $('#VendNoInputDisplay').text();
var vendNo = $('#VendorNumber').val();
var invNo = $('#InvNoInput').val();
var error;
$.ajax({
type: "POST",
aSync: false,
url: "PaymentRequest.aspx/CheckInvoiceExists",
data: JSON.stringify({
vendorNumber: vendNo,
invoiceNumber: invNo
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d) {
$('#ErrorList').text(GetErrorText("invoiceNumberExists"));
$('#InvNoInput').focus().select();
$('#InvNoInput').addClass('error invExists');
}
else
{
$('#InvNoInput').removeClass('error invExists');
ClearErrors();
}
},
error: function (jqXHR, textStatus, errorThrown)
{
$('#ErrorList').text(errorThrown);
}
});
}
這裏是我的本地機器的響應頭:
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Mon, 26 Jan 2015 18:18:36 GMT
Content-Length: 11
從開發:
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 25586
Date: Mon, 26 Jan 2015 18:30:40 GMT
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Cache-Control: private
當我調試它,它去到$就調用的錯誤功能。
errorThrown : SyntaxError: Unexpected token <
jzXHR.responseText : [HTML of the page]
textStatus: "parserror"
當我打開OP的CheckInvoiceExist包我看到:
Response is the current page.
The request payload is something like this {"vendorNumber":"0007000005","invoiceNumber":"Test1-12"}
@edit 我嘗試添加我的網絡方法以下行,但它並沒有區別
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "json")]
@edit 我試着用PageMethods代替使用$ .aJax調用。然後我嘗試了以下測試:
function Test(response)
{
alert(response);
}
PageMethods.CheckInvoiceExists("0007000005","Test1-12",Test);
在警報消息我再次得到了該網頁的HTML ...
你可以發佈'RequestEntry.CheckInvoiceExists'方法嗎? – mattytommo 2015-01-26 18:41:49
'content-length:11' v.s. 'content-length:25586' ...我猜v6頁面無論什麼原因都沒有正確執行。 – 2015-01-26 18:44:34
@mattytommo RequestEntery.CheckInvoiceExists僅調用數據庫中的存儲過程,然後將該結果返回給web方法。 – 2015-01-26 18:48:11