0

I have tried to use the following code to retrieve the data from the webserver (test.asmx.cs) but somehow this is always throw the error to me... does anyone know what is it going wrong?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

namespace Test
{
    /// <summary>
    /// Summary description for autocomplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class autocomplete : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public static string streetNameSearch(int id)
        {

            return "Melbourne|North Melbourne|South Melbourne|Richmond|North Richmond";
        }
    }
}

And the following jquery code have placed under pgTest.aspx

$("#example").keyup(function () {
    $.ajax({
        type: "POST",
        url: "pgTest.aspx/streetNameSearch",
        data: '{"id":"' + 1 + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var returnData = data.d;
            alert(returnData)

    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(ajaxOptions);
    },
    timeout: function (data) {
        alert("time out");
    }
});
});

enter image description here

4
  • What is the error that is being thrown? Commented Apr 6, 2011 at 3:18
  • @jon3laze: the error is just 'error', Commented Apr 6, 2011 at 3:24
  • put a breakpoint in your method streetNameSearch and check whether it is called? Commented Apr 6, 2011 at 3:34
  • @jayantha, this is gone through to the error part and not even manage to call to the method streetNameSearch Commented Apr 6, 2011 at 3:50

3 Answers 3

1

Uncomment this line:

// [System.Web.Script.Services.ScriptService]

The ScriptService attribute enables all of an ASMX service’s methods to respond with raw JSON.

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to uncommenting the ScriptService line, why is your $.ajax() method targeting an ASPX path while the code you posted is from an ASMX service? That could definitely cause a problem.

Comments

0

Try this

     public static JsonResult streetNameSearch(string id)
        {

            return Json("Melbourne|North Melbourne|South Melbourne|Richmond|North Richmond");
        }

And in your javascript change like this

data: {id:"1"}

1 Comment

@Jayanthan: seem like JsonResult and Json() is not support in .net c# webservice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.