【问题标题】:how to get double object using json or jquery?如何使用 json 或 jquery 获取双对象?
【发布时间】:2016-07-30 07:13:26
【问题描述】:

我尝试通过 json 或 jquery ajax 从 webservice 获取双 DataTable

WebService 方法:

[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;
}

Js页面:

$.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 存储在 DataSet 中。

【问题讨论】:

  • 你试过了......发生了什么?请添加更多详细信息。有什么问题?服务是做什么的?你想完成什么?
  • 嗨,Sami,感谢您的宝贵回复,我再次正确编辑了我的问题,您可以看到并帮助我,Thanx 提前:)

标签: jquery asp.net json


【解决方案1】:

首先,您的 $.ajax 调用是错误的,特别是您通过 custId_ 参数的方式。此外,dataType 不会在 POST 调用中使用,contentType 也可以排除在外。

像这样更改您的 $.ajax 调用,您现在将能够在[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");
    }
});

其次,看看你实例化SqlConnection对象的方式:

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

“...”不是有效的连接字符串。您需要将连接字符串存储到您的 SQL 数据库中的Web.config 文件中,如下所示:

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

并像这样从 C# 代码中访问它:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2013-07-12
    相关资源
    最近更新 更多