2015-01-26 12 views
5

Veritabanında metin değerinde bir değer olup olmadığını kontrol etmek için istemci tarafında aradığım basit bir webmethod yazdım. Yerel olarak iyi çalışıyor, ancak bunu Geliştirme ortamımıza taşıdığımda, sayfanın tüm HTML'sini yanıtta döndürüyor. Fark ettiğim tek şey yerel olarak Response.Server'ın IIS7.5 olduğunu, ancak Dev sunucumuzda IIS6 olduğunu. İşte

benim kodudur:

Sunucu Kod

Müşteri Kodu

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); 

    } 
}); 

} Burada

benim yerel makineden yanıt başlığıdır

[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; 
     } 
    } 

:

Dev itibaren

: ben bunu hata ayıklama

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 

, bu $ .ajax aramanın hata fonksiyonuna gider.

errorThrown : SyntaxError: Unexpected token < 
jzXHR.responseText : [HTML of the page] 
textStatus: "parserror" 
Gördüğüm op CheckInvoiceExist paketi açmak

:

Response is the current page. 
The request payload is something like this  {"vendorNumber":"0007000005","invoiceNumber":"Test1-12"} 

@edit benim web yöntemi Aşağıdaki satırı ekleyerek çalıştı , ama bir fark yaratmak değildi

[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "json")] 

@edit $ .aJax çağrılarını kullanmak yerine PageMethods kullanmayı denedim. Ben bir kez daha sayfa için HTML var uyarı mesajında ​​

function Test(response) 
{ 
    alert(response); 
} 

PageMethods.CheckInvoiceExists("0007000005","Test1-12",Test); 

...

+0

, "RequestEntry.CheckInvoiceExists" yöntemini gönderebilir misiniz? – mattytommo

+0

'içerik uzunluğu: 11' v.s. 'content-length: 25586' ... Sanırım v6 sayfası, herhangi bir sebepten dolayı düzgün bir şekilde yürütülüyor. –

+0

@mattytommo RequestEntery.CheckInvoiceExists, veritabanından saklı yordamı çağırır ve ardından bu sonucu webmethod'a döndürür. –

cevap

0

Değişim json dönmek için sunucu yöntemi:: Eh

[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

anahtardı "jQuery" ile "JSON", "JST" ile "WebMethod" öğesini çağırdığında, zaten "JSON" olarak döndürülür. –

+0

ResponseFormat.Json'ı zaten eklemeye çalıştım –

2

Sonra aşağıdaki testi çalıştı Kafamı tam bir gün boyunca masama yasladıktan sonra neyin yanlış olduğunu anladım.

Ben

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

O çizgi olup olmadığını IIS7.5 umursamıyor tahmin benim web config benim <system.web> aşağıdaki anahtarı eksikti, ama IIS6 oradaki hattı olması gerekiyor Web metotlarının çalışması için.

Herkese yardım için teşekkürler!

+0

Sizin gibi, kafamı bir çok saat boyunca masama çarpıyordum ve cevabınız başımı kurtardı! – krlzlx

+0

Senin gibi, kafamı masamın karşısına saatlerce çalıyordum ve cevabınız kafamı kurtardı! Çok teşekkür ederim. Mutlu Kodlama! –

İlgili konular