2015-01-26 55 views
5

我寫了一個簡單的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 ...

+0

你可以發佈'RequestEntry.CheckInvoiceExists'方法嗎? – mattytommo 2015-01-26 18:41:49

+0

'content-length:11' v.s. 'content-length:25586' ...我猜v6頁面無論什麼原因都沒有正確執行。 – 2015-01-26 18:44:34

+0

@mattytommo RequestEntery.CheckInvoiceExists僅調用數據庫中的存儲過程,然後將該結果返回給web方法。 – 2015-01-26 18:48:11

回答

0

更改服務器的方法來返回JSON:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
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; 
     } 
    } 
+0

當用'json'將'WebMethod'作爲'dataType'與'jQuery'調用時,它已經返回爲'JSON'。 – 2015-01-26 19:12:30

+0

我試着添加ResponseFormat.Json已經 – 2015-01-26 19:34:47

2

好在將我的頭撞到我的辦公桌一整天后,我終於明白出了什麼問題。

我錯過了我的<system.web>以下關鍵在我的web配置

<httpModules> 
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
</httpModules> 

我想這IIS7.5不關心,如果該行是有或沒有,但IIS6需要有該行有爲了網絡方法的功能。

謝謝大家的幫助!

+0

就像你,我在我的桌子上撞了很多小時,你的回答挽救了我的腦袋! – krlzlx 2015-08-10 11:31:37

+0

和你一樣,我的頭靠在桌子上呆了好幾個小時,你的回答挽救了我的腦袋! 非常感謝。 快樂編碼! – 2016-10-19 13:14:07

相關問題