This article uses jquery's ajax to call Webservice to return a Json array. json data is a small lightweight javascript data, and real-time interaction is more powerful than xml.
This article uses jquery ajax to call webservice to return a json array. json data is a small lightweight data for webpage special effects. Real-time interaction is more powerful than xml.
Json data
{'Employee': [{'name': 'john', 'sex': 'man', 'age': '25'}, {'name ': 'Tom ', 'sex': 'man', 'age': '21'}, {'name': 'Mary ', 'sex': 'Woman ', 'age': '21'}]}
// Jquery calls webservice to import data
Function loaddata (){
Var studentdata = collectiondata ();
$. Ajax ({
Url: "importdataservice. asmx/importstu ",
Type: "post ",
Contenttype: "application/json; charset = UTF-8 ",
Datatype: "json ",
Data: "{'students': [{'name': 'kobe', 'sex': 'boys', 'age': '20'}, {'name ': 'Mary ', 'sex': 'girl', 'age': '19'}]} ",
Success: function (result ){
Alert (result. d );
},
Error: function (e ){
Alert (e. responsetext );
}
});
}
/// <Summary>
///
/// </Summary>
/// <Param name = "students"> </param>
/// <Returns> </returns>
[Webmethod]
[Scriptmethod (responseformat = responseformat. json)]
Public string importstu (dictionary <string, string> [] students)
{
If (students. length = 0)
{
Return "no data! ";
}
Else
{
Try
{
Foreach (dictionary <string, string> stu in students)
{
// Construct a new student object.
Student = new student ();
// Assign values to the properties of the newly constructed student object.
Foreach (string key in stu. keys)
{
Switch (key)
{
Case "name ":
Student. name = stu [key];
Break;
Case "sex ":
Student. sex = stu [key];
Break;
Case "age ":
Int age;
If (int32.tryparse (stu [key], out age ))
{
Student. age = age;
}
Else
{
Student. age = 0;
}
Break;
Default:
Break;
}
}
}
Return "Student import successful! ";
}
Catch
{
Throw new exception ("failed to import student! ");
}
}
}