$ .ajax Return HTML instead of results

I wrote a simple web method that I call on the client side to check if a value exists in the database when the text changes. It works fine locally, but when I port it to our development environment, it returns all the page HTML in response. The only thing I noticed is that locally Response.Server is IIS7.5, but on our Dev server it is IIS6.

Here is my code:

Server code

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

Client code

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

}

Here is the response header from my local machine:

 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 

From Dev:

 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 

When I debug it, it goes to the $ .ajax call error function.

 errorThrown : SyntaxError: Unexpected token < jzXHR.responseText : [HTML of the page] textStatus: "parserror" 

When I open the CheckInvoiceExist package, I see:

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

@edit I tried adding the following line above my web method, but this did not affect

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

@edit I tried using PageMethods instead of using calls to $ .aJax. Then I tried the following testing:

 function Test(response) { alert(response); } PageMethods.CheckInvoiceExists("0007000005","Test1-12",Test); 

In the warning message, I again received the HTML for the page ...

+5
source share
2 answers

Well, after I hit my head on my desk for the whole day, I finally realized what happened.

I was missing the following key in my <system.web> in my web configuration

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

I assume IIS7.5 does not care if this line exists or not, but IIS6 needs to have this line there in order for web methods to function.

Thank you all for your help!

+2
source

Change your server method to return 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
source

Source: https://habr.com/ru/post/1211945/


All Articles