Ajax call doesn't hit ASPX Page Method
Trying to call a WebMethod on an ASPX page from jQuery.Ajax call on client. Have seen this done on several web pages and instructionals. Problem seems to be that the actual web method is never 'hit' although the ajax call returns success, all that is in the data returned is the html of the page that made the call.
Using VS2010, jQuery 1.8.1
PAGE CODE:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebMethodReturnsDataTableJSON.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <script src="scripts\jquery-1.8.1.js"></script>
- <script>
- function Button1_onclick()
- {
- var returnedJson = "";
- //$.getJSON("WebForm1.aspx/GetDataTable",returnedJson,ReturnedJson);
- $.ajax({
- type: 'POST',
- url: 'WebForm1.aspx/GetDataTable',
- data:'{}',
- datatype: 'json',
- error: (function (jqXRSMStruct,status,htmlmessage) {alert (htmlmessage) }),
- success: (function(response){alert ("success")})
- });
- };
- function ReturnedJson(data, textStatus, jqXHR)
- {
- $("Button1").text = "Finished";
- };
- </script>
- </head>
- <body>
-
- <form id="form1" runat="server">
- <div>
-
- <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" </div>
- </form>
- </body>
ASPX page code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.Services;
- using System.Web.Script.Serialization;
- using System.Data;
- using System.Data.SqlClient;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using System.IO;
- namespace WebMethodReturnsDataTableJSON
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- [WebMethod(EnableSession=false)]
- public static string GetJSONTable()
- {
- SqlConnection dbConnection;
- SqlCommand dbCommand;
- SqlDataAdapter dbAdapter;
- DataTable dbTable = new DataTable();
- string connectionString;
- string serializedDataTable;
- connectionString = "Server=HQSQLDEVSRVR;Database=Tapemacs;User ID=sqlrunner;Password=d8tabase;";
- dbConnection = new SqlConnection(connectionString);
- dbConnection.Open();
- dbCommand = new SqlCommand("SELECT * FROM RejectOrders", dbConnection);
- dbAdapter = new SqlDataAdapter(dbCommand);
- dbAdapter.Fill(dbTable);
- JsonSerializer serializer = new JsonSerializer()
- {
- NullValueHandling = NullValueHandling.Include,
- ObjectCreationHandling = ObjectCreationHandling.Replace,
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore
- };
- serializer.Converters.Add(new DataTableConverter());
- StringWriter stringWriter = new StringWriter();
- JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter);
- jsonWriter.Formatting = Formatting.None;
- jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;
- jsonWriter.QuoteChar = '"';
- serializer.Serialize(jsonWriter,dbTable);
- serializedDataTable = stringWriter.ToString();
- jsonWriter.Close();
- stringWriter.Close();
- return serializedDataTable;
- }