使用JQuery PoST方法调用.NET服务器端组件

时间:2014-06-30 13:58:51

标签: c# jquery .net server-side

真的坚持这种恼人的东西!! 请帮忙!

我的问题如下: 我使用JQuery $ .ajax POST方法调用我的.NET(C#)服务器端组件,我的服务器端组件将一些静态/硬编码数据返回给我的JQuery客户端。 但是当我在我的.NET(C#)服务器端组件中查询数据库操作并尝试返回从数据库中获取的记录时,我会看到" 500内部错误(jquery-1.10.2.js) "在我的Chrome控制台中,我无法从服务器端获取数据。 请帮朋友们! 非常困难!

这是我的JQuery代码:

function invokeServerSideComponent()
{
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetData",
        data: {parameters : "B,A,L"},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: OnFailure
    });
}

function OnSuccess(response) {
    alert("SUCCESS : " + response.d);
};

function OnFailure(response) {
    alert('FAILURE : ' + response.d);
};

这是我的服务器端C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    //public static string GetData()
    public static string GetData(string parameters)
    {
        System.Diagnostics.Debug.Write("Locators requested from Client are : " + parameters);

        string dataString = "";
        string mainString = "";

        SqlConnection mySqlConn = new SqlConnection("user id=userId;"
            + "password=password;"
            + "server=serverName;"
            + "Trusted_Connection=YES;"
            + "database=Database name;"
            + "connection timeout=30;");

        try
        {
            mySqlConn.Open();
            System.Diagnostics.Debug.Write("Connection opened....");
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.Write("Exception in opening Connection with DB...");
        }

        System.Diagnostics.Debug.Write("Before myReader1....");

        try
        {
            SqlDataReader myReader1 = null;
            SqlCommand myCommand1 = new SqlCommand("SELECT * FROM TEST_TABLE", mySqlConn);

            myReader1 = myCommand1.ExecuteReader();

            System.Diagnostics.Debug.Write("After executeReader1..........");



            while (myReader1.Read())
            {
                string indviRow = "\"" + myReader1["id"].ToString() + "\"," +
                      "\"" + myReader1["Locator_name"].ToString() + "\"," +
                      "\"" + myReader1["Address_line1"].ToString() + "\"," +
                      "\"" + myReader1["Address_line2"].ToString() + "\"," +
                      "\"" + myReader1["City"].ToString() + "\"," +
                      "\"" + myReader1["State"].ToString() + "\"," +
                      "\"" + myReader1["Country"].ToString() + "\"," +
                      "\"" + myReader1["Postcode"].ToString() + "\"," +
                      "\"" + myReader1["Locator_type"].ToString() + "\"," +
                      "\"" + myReader1["Status"].ToString() + "\"," +
                      "\"" + myReader1["Hrs_of_working"].ToString() + "\"," +
                      "\"" + myReader1["Xcoord"].ToString() + "\"," +
                      "\"" + myReader1["Ycoord"].ToString() + "\"," +
                      "\"" + myReader1["uuid"].ToString() + "\",";
                      //"\"" + columnValueReader["uuid"].ToString() + "\"," +
                      "\"" + "100".ToString() + "\"," +
                      "\"" + "9999".ToString() + "\",";

                indviRow = indviRow + "\"" + "100".ToString() + "\"," + "\"" + "9999".ToString() + "\",";
                System.Diagnostics.Debug.Write("Before Trim: " + indviRow);
                indviRow = indviRow.TrimEnd(',');
                System.Diagnostics.Debug.Write("After Trim: " + indviRow);
                indviRow = indviRow + "\n";
                dataString = dataString + indviRow;
            }
            System.Diagnostics.Debug.Write("After while loop..............");
            System.Diagnostics.Debug.Write("The rows are : " + dataString);
            myReader1.Close();
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.Write("Exception in reading RBLLocator test details...");
        }

        try
        {
            System.Diagnostics.Debug.Write("Closing mySqlConn............");
            mySqlConn.Close();
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.Write("Some exception in closing SQL Server connection....");
        }

        String string1 = "\"id\",\"Fcilty_nam\",\"Address_line1\",\"Address_line2\",\"City\",\"State\",\"Country\",\"Postcode\",\"Locator_type\",\"Status\",\"Hrs_of_bus\",\"Wheelchair\",\"Display_wd\",\"Xcoord\",\"Ycoord\",\"uuid\",\"result\",\"Audio\"";

    // This one works! Harcoded data if returned to Client.
    String string2 = "\"191\",\"KANDIVALI\",\"Ratnakar Bank\",\"near Mahatma Gandhi Rd\",\"Manek Nagar\",\"MAHARASHTRA\",\"INDIA\",\"400067\",\"B\",\"A\",\"09:00-17:00 Monday to Friday, excluding Public Holidays\",\"YES\",\"1.5\",\"72.844359\",\"19.2054962\",\"1\",\"100\",\"9999\"";


    mainString = string1 + "\n" + dataString; // dataString is a String generated from database record, if this one returned to Client I get 500 Internal Server Error

        //mainString = string1 + "\n" + string2; // string2 is hardcoded! This works!

        System.Diagnostics.Debug.Write("The mainString : " + mainString);
        return mainString;
    }
}

所以这些是我的JQuery Code&服务器端C#代码!

请尽快帮助!提前谢谢!

0 个答案:

没有答案