Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:17:11+00:00 2026-05-20T18:17:11+00:00

I have a simple ajax request that I am trying to call a web

  • 0

I have a simple ajax request that I am trying to call a web service I created. The web service code is never being executed. I got this code sample from the net and I think maybe I am calling the url wrong in the jquery i dunno?

here is the jquery

<script type="text/javascript">
    $(document).ready(function() { 

    alert("Hi");
        $("#btnTitleQuery").bind("click", function() {
            $("#query_results").empty();
            $("#query_results").append('<table id="ResultsTable" class="BooksTable"><tr><th>BookNum</th><th>Title</th><th>Author</th></tr>');
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "BookServices.asmx/GetBooksByTitle",
                data: '{ strTitle: "' + $("#txtTitle").val() + '" }',
                dataType: "json",
                success: function(msg) {
                    var c = eval(msg.d);
                    for (var i in c) {
                        $("#ResultsTable tr:last").after("<tr><td>" + c[i][0] + "</td><td>" + c[i][1] + "</td><td>" + c[i][2] + "</td></tr>");
                    }
                }
            });
        })
        $("#btnAuthorQuery").bind("click", function() {
            $("#query_results").empty();
            $("#query_results").append('<table id="ResultsTable" class="BooksTable"><tr><th>BookNum</th><th>Title</th><th>Author</th></tr>');
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "BookServices.asmx/GetBooksByAuthor",
                data: '{ strAuthor: "' + $("#txtAuthor").val() + '" }',
                dataType: "json",
                success: function(msg) {
                    var c = eval(msg.d);
                    for (var i in c) {
                        $("#ResultsTable tr:last").after("<tr><td>" + c[i][0] + "</td><td>" + c[i][1] + "</td><td>" + c[i][2] + "</td></tr>");
                    }
                }
            });
        })
    }); 
</script>

The web service is in my root directory.

using System;

 using System.Web;

 using System.Collections;

 using System.Web.Services;

using System.Web.Services.Protocols;

 using System.Data;

  using System.Data.Odbc;

   using System.Web.Script.Serialization;

 using System.Web.Script.Services;

namespace TryWillJSON
{

 [WebService(Description = "Web services to query the book database.", Namespace = "http://www.williamsportwebdeveloper.com/")]

  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   [ScriptService]

 public class BookServices : System.Web.Services.WebService {



       public BookServices () {



        //Uncomment the following line if using designed components 

         //InitializeComponent(); 

     }



     [WebMethod(Description = "Gets the books matching part of a title.")]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

     public string GetBooksByTitle(string strTitle) {

        OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString);

         OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Title LIKE '%" + strTitle + "%' ORDER BY BookNum;", objConnection);

         DataSet objDataSet = new DataSet();

         OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand);

         objDataAdapter.Fill(objDataSet, "reading");

           objConnection.Close();



         // Create a multidimensional jagged array

        string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];

        int i = 0;

         foreach (DataRow rs in objDataSet.Tables[0].Rows)

        {

            JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() };

             i = i + 1;

          }



        // Return JSON data

         JavaScriptSerializer js = new JavaScriptSerializer();

      string strJSON = js.Serialize(JaggedArray);

       return strJSON;

      }



     [WebMethod(Description = "Gets the books matching part of an author's name.")]

   [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public string GetBooksByAuthor(string strAuthor)

       {

         OdbcConnection objConnection = new OdbcConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Books"].ConnectionString);

         OdbcCommand objCommand = new OdbcCommand("SELECT * FROM reading WHERE Author LIKE '%" + strAuthor + "%' ORDER BY BookNum;", objConnection);

         DataSet objDataSet = new DataSet();

        OdbcDataAdapter objDataAdapter = new OdbcDataAdapter(objCommand);

        objDataAdapter.Fill(objDataSet, "reading");

        objConnection.Close();



          // Create a multidimensional jagged array

         string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];

        int i = 0;

        foreach (DataRow rs in objDataSet.Tables[0].Rows)

      {
             JaggedArray[i] = new string[] { rs["BookNum"].ToString(), rs["Title"].ToString(), rs["Author"].ToString() };

             i = i + 1;

        }



          // Return JSON data

        JavaScriptSerializer js = new JavaScriptSerializer();

         string strJSON = js.Serialize(JaggedArray);

         return strJSON;

     }



   }
}

I can see the alert hits, but in firebug when I check whats in the “msg” parameter of the jquery function firebug tells me this “ReferenceError: msg is not defined”

Here is where I am getting the code:
williamsportwebdeveloper.com/cgi/wp/?p=494

If anyone has time can you see if you can set it up correctly in visual studio

Here is a fiddle I setup as well for what its worth
http://jsfiddle.net/npl77/U33XS/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:17:11+00:00Added an answer on May 20, 2026 at 6:17 pm

    Your parameters do not have the proper single tick marks around the property names
    Rewrite them to look like this:

    data: "{ 'strAuthor': '" + $("#txtAuthor").val() + "' }",
    
    data: "{ 'strTitle': '" + $("#txtTitle").val() + "' }",
    

    because of this, your parameters are not matching up when being checked on the server-side. Therefore, your code is never executed. That is the first mistake I found … there could be more,

    check that then we’ll see.

    Also, you can use JSON natively in javascript which will make this tedious and error proned string building process hands off. You just need to get a copy of the small JSON library here and add it to your project (it’s only a javascript file btw).

    http://sourceforge.net/projects/json-lib/files/

    Here is an example

     //using native JSON
        var per = { FirstName: $('#fName').val(),
                    LastName : $('#lName').val(),
                    Address : $('#address').val(),
                    City : $('#city').val(),
                    State : $('#state').val(),
                    ZipCode : $('#zip').val()
                  };
    
       $.ajax(
       {
           type: "POST",
           url: "Default.aspx/GetPersonObj",
           data: JSON.stringify(per),  //<---- right here the magic happens
           contentType: "application/json; charset=uft-8",
           dataType: "json",
           success: function (rsp) { SetPerson(rsp); },
           error: function (rsp)
           {
               alert(rsp.responseText); //<--- this is always good to add, that way you can get some feedback from the ajax call about what the hell went wrong.
           }
       });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple $.ajax request that I am trying to fetch some HTML
I have a simple ajax call: $.ajax({url: my_url_here, dataType: 'text', success: function(data, textStatus) {
I have seen simple example Ajax source codes in many online tutorials. What I
I had a realtivley simple ajax application, which I have broken up to be
I have a simple HTML. I am using the JQuery for AJAX purpose. Now,
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I've been trying to make an AJAX request to an external server. I've learned
I have a new web app that is packaged as a WAR as part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.