Member Avatar for amby

Hi,
I have to pass string entered in the input text to server method calling through jquery ajax. But its not going through. can please somebody tell me what i m doing wrong here. Below is the code:

$.ajaxSetup({ cache: false //timeout: 1000000 }); function concatObject(obj) { strArray = []; //new Array for (prop in obj) { strArray.push(prop + " value :" + obj[prop]); } return strArray.join(); } //var Eid = "stephen.gilroy1"; function testCAll() { //var ntid = $('#Eid').val(); $.ajax({ type: "POST", url: "Testing.aspx/SendMessage", //data: "{'ntid':'stephen.gilroy1'}", //working data: "{'ntid': $('#Eid').val()}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) { alert(result.d); resultData = eval("(" + result.d + ")"); $("#rawResponse").html(result.d); //$("#response").html(resultData.sn); }, error: function(result) { alert("jQuery Error:" + result.statusText); } }); }

above is js file and below is its aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Testing.aspx.cs" Inherits="Testing" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="jquery.js" type="text/javascript"></script> <script src="Testing.js" type="text/javascript"></script> <script src="json2.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div> Employee's NTID: <input type="text" id = "Eid" name="Employee_NTID" /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <br /> <br /> <input type="button" onclick="testCAll()" value = "Search"/> <div id="rawResponse"></div> <hr /> <div id="response"></div> </div> </form> </body> </html>
Member Avatar for vitana

json data is not correct. it should be

data: "{'ntid': "+$('#Eid').val()+"}", 

or

data : {'ntid' : $('#Eid').val()}, 

dependent on your server side functionality

Member Avatar for amby

thanks but still problem is here. i can show my server code too if it helps u in determining my problem. but server side code is running good when i pass values like this:
data: "{'ntid':'stephen.gilroy1'}",

it only making problem when passing value of input textbox. server side method expects string value as an argument. may be there is the problem, that i have to convert it to string first in the server side method then do any processing. below is my C# code:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; //using System.IO; //using Newtonsoft.Json; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Collections; public partial class Testing : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { al2c00.ldap lp = new al2c00.ldap(); DataTable dt = lp.GetEmployeeDetailsBy_NTID("650FA25C-9561-430B-B757-835D043EA5E5", "stephen.gilroy1"); GridView1.DataSource = dt; GridView1.DataBind(); } //[WebMethod()] //[ScriptMethod()] [WebMethod] public static string SendMessage(string ntid) { try { al2c00.ldap ws = new al2c00.ldap(); Hashtable htPeople = new Hashtable(); DataTable dt = ws.GetEmployeeDetailsBy_NTID("650FA25C-9561-430B-B757-835D043EA5E5", ntid); //Creating StringBuilder array for storing keys StringBuilder[] empKeys = new StringBuilder[70]; for (int i = 0; i < empKeys.Length; i++) { empKeys[i] = new StringBuilder(); } //Creating stringbuilder array for storing key values StringBuilder[] empDetails = new StringBuilder[70]; for (int i = 0; i < empDetails.Length; i++) { empDetails[i] = new StringBuilder(); } //putting datatable data to empKeys and empDetails array int inc = 0; int j = 0; foreach (DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) { empKeys[inc].Append(dc.ColumnName); inc++; } foreach (DataColumn dc in dt.Columns) { empDetails[j].Append(dr[dc]); j++; } } for (int k = 0; k < 70; k++) { htPeople.Add(empKeys[k].ToString(), empDetails[k].ToString()); } //htPeople.Add(empKeys[2].ToString(), empDetails[2].ToString()); JavaScriptSerializer jss = new JavaScriptSerializer(); string output = jss.Serialize(htPeople); return output; } catch(Exception ex) { return ex.Message + "-" + ex.StackTrace; } } }
Member Avatar for vitana

if $('#Eid').val() is string then quote it

data: "{'ntid': '"+$('#Eid').val()+"'}"

more flexible way is custom routing
read my article on it. there you can process json object as is without string representation it

Member Avatar for amby

thanks a lot! it works now. thank u so much

Member Avatar for Bhavna_4
Hi, I am facing similar issue, Please guide:- $.ajax({ type: "POST", url: "HomePage.aspx/SaveVal", data: "{'ShortCode': '" + $('#ShortCode').val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response.d); } }); What is wrong with this? please reply I am getting error POST http://localhost:18839/HomePage.aspx/SaveVal 500 (Internal Server Error)
Member Avatar for anand9796

The following way will work

data: { ntid: $("#Eid").val() }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.