views:

50

answers:

2

Hello everyone..

i am using jqery ajax to calling a web service method but is is not doing and genrating error..

the code is here for jquery ajax in asp page

var indexNo = 13; //pass the value

    $(document).ready(function() {
        $("#a1").click(function() {
         $.ajax({
                type: "POST",
                url: "myWebService.asmx/GetNewDownline",
                data: "{'indexNo':user_id}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                $("#divResult").text(msg.d);
                }
        });
    });
    });

and this is the is web service method

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Web.Script.Serialization;
using TC.MLM.DAL;
using TC.MLM.BLL.AS;

/// <summary>
/// Summary description for myWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 myWebService : System.Web.Services.WebService
{

    public myWebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

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

        IndexDetails indexDtls = new IndexDetails();
        indexDtls.IndexNo = "13";
        DataSet ds = new DataSet();
        ds = TC.MLM.BLL.AS.Index.getIndexDownLineByIndex(indexDtls);
        indexNoDownline[] newDownline = new indexNoDownline[ds.Tables[0].Rows.Count];
        for (int count = 0; count <= ds.Tables[0].Rows.Count - 1; count++)
        {
            newDownline[count] = new indexNoDownline();
            newDownline[count].adjustedid = ds.Tables[0].Rows[count]["AdjustedID"].ToString();
            newDownline[count].name = ds.Tables[0].Rows[count]["name"].ToString();
            newDownline[count].structPostion = ds.Tables[0].Rows[count]["Struct_Position"].ToString();
            newDownline[count].indexNo = ds.Tables[0].Rows[count]["IndexNo"].ToString();
            newDownline[count].promoterId = ds.Tables[0].Rows[count]["PromotorID"].ToString();
            newDownline[count].formNo = ds.Tables[0].Rows[count]["FormNo"].ToString();
        }
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        JavaScriptSerializer js = new JavaScriptSerializer();
        string resultedDownLine = js.Serialize(newDownline);
        return resultedDownLine;

    }
    public class indexNoDownline
    {
        public string adjustedid;
        public string name;
        public string indexNo;
        public string structPostion;
        public string promoterId;
        public string formNo;

    }
}

please help me something.

A: 

There is a problem with your input JSON data. Instead of manual serialization you should try to use the builtin JSON class. Ex.

$.ajax({
   ...
   data: JSON.stringify({ indexNo: user_id }),
   ...
 });

This should fix your problem.

egyedg
A: 

Hello!

You should change the art how you implement serialization in the server. Just add ScriptMethod attribute with ResponseFormat = ResponseFormat.Json. Using of HTTP GET is also possible with respect of additional (optional) attribute UseHttpGet = true

[WebMethod]
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetNewDownline(string indexNo)

The database access can be also improved if you will use SqlConnection, SqlCommand and SqlDataReader classes or another versions of DbDataReader depends of you database source (OleDbDataReader, OracleDataReader, OdbcDataReader, DataTableReader). If you prefer work with strong typed data, then SqlDataAdapter generated by Visual Studio will be better version of DataSet. To do this just add a new Item to your profject, choose "Data" / "DataSet" and then add a Query or TableAdapter.

If you decide to use HTTP GET you should don't make corresponding modification of your web.config file.

<configuration>
  <!-- ... -->
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
      </protocols>
    </webServices>
    <!-- ... -->
  </system.web>
</configuration>

About usage of JSON.stringify I have the same opinion like "egyedg".

I recommend you to look following links:

http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583

http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086

http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working/2656543#2656543

Oleg