简体   繁体   中英

how to get double object using json or jquery?

I tried out to get double DataTable from webservice by json or jquery ajax like

The WebService Method :

[WebMethod]
public DataSet FareAccpted_(string custId)
{
    DataSet ds = new DataSet();
    List<fareAccptedList> details = new List<fareAccptedList>();

    using (SqlConnection con = new SqlConnection("..."))

    {
        using (SqlCommand cmd = new SqlCommand("proc_FareAcceptedC", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@customerId", SqlDbType.Int).Value = custId;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
        }
    }
    return ds;
}

The Js page :

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "../lpService.asmx/FareAccpted_",
    data: JSON.stringify({ custId: custId_ }),
    dataType: "json",
    success: function (data) {
        alert('Length : '+data.d.length)

    },
    error: function (result) {
        alert("FareAcceptedC" + "Error");
    }
});

DataTables stores in DataSet.

Firstly, your $.ajax call is wrong, specifically the way you are passing through the custId_ parameter. Also the dataType is not used in the POST call and the contentType can be excluded as well.

Change your $.ajax call like this and you will now be able to hit a breakpoint in the [WebMethod] :

$.ajax({
    type: "POST",
    url: "../lpService.asmx/FareAccpted_",
    data: { "custId": custId_ },
    success: function (data) {
        alert('Length : ' + data.d.length)
    },
    error: function (result) {
        alert("FareAcceptedC" + "Error");
    }
});

Secondly, look at the way you're instantiating the SqlConnection object:

using (SqlConnection con = new SqlConnection("..."))

"..." is not a valid connection string.You need store the connection string to your SQL database in the Web.config file like this:

  <connectionStrings>
    <add name="connectionString" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=UsersDatabase;Data Source=MyServerName"/>
  </connectionStrings>

And access it from C# code like this:

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM