I am passing the Value to Array Element in WCF service. Here I am Using jQuery.
My functions are:
In Service.cs:
public class User
{
Dictionary<int, string> users = null;
public User()
{
users = new Dictionary<int, string>();
users.Add(1, "apple");
users.Add(2, "orange");
users.Add(3, "lemon");
users.Add(4, "grape");
}
public string[] GetUser(int Id)
{
var user = from u in users
where u.Key == Id
select u.Value;
return user.ToArray<string>();
}
In jQuery:
function CallService() {
$.ajax({
type: Type,
url: Url,
data: Data,
contentType: ContentType,
dataType: DataType,
processdata: ProcessData,
success: function(msg) {
ServiceSucceeded(msg);
},
error: ServiceFailed
});
}
My first function I declare like this:
function WCFJSON() {
var uesrid = "2";
Type = "POST";
Url = "Service.svc/GetUser";
Data = '{"Id": "' + uesrid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; ProcessData = true;
CallService();
}
This function I called like below:
$(document).ready(
function() {
WCFJSON();
}
);
My second function is:
function temp()
{
//var id=parseInt($('#txtinput').val();
var id="5";
Type="POST";
Url="Srvice.svc/GetUser";
Data='{"Id":"'+id+'"}';
ContentType="application/json;charset=utf-8";
DataType="json";ProcessData=true;
CallService();
}
I called this function by button onclient click, like below:
<asp:Button ID="btnsumbit" runat="server" Text="submit" OnClientClick ="temp();" />
My problem is when I ran this program, I got only got a result from first function (“WCFJSON();”), other function of “temp();” I didn’t get any result. I don’t know what I am missing? Can anyone resolve this?
should be:
This is supposed to be an object, and
$.ajaxwill serialize it.In temp(), change:
to: