ต่อจากของเดิม http://kungtee.rmutp.ac.th/index.php/wcf-restful-json/
ต่อจากครั้งที่แล้วทำ table และปุ่ม

<!doctype html> <html lang="en"> <head> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript" src="myscript.js"></script> </head> <body> <input type="text" id="hello" value="" /><br/> <input type="text" id="studentid" value="" /><input type="button" id="submit" value="submit"/><br/> <input type="text" id="firstname" value="" /><br/> <input type="text" id="lastname" value="" /><br/> <input type="button" id="getallstudent" value="get all student"/><br/> <table id="studentlist"> </table> </body> </html>
$('#getallstudent').click(function(){
$.getJSON('http://localhost:8000/getAllStudent?callback=?',
null,
function(data){
$.each(data,function(i,student){
$('<tr><td>'+student.firstname+'</td><td>'+student.lastname+'</td></tr>').appendTo('#studentlist');
//alert(student.firstname);
});
}
);
});
การ Insert Data ให้กลับไปแก้ WCF ในส่วนของ interface iService1 แล้วเพิ่มโค้ดลงไป
แก้ไข โค้ด
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WCF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WebServiceHost wsh = new WebServiceHost(
typeof(Service1), new Uri("http://localhost:8000"));
// JSONP
WebHttpBinding whb = new WebHttpBinding();
whb.CrossDomainScriptAccessEnabled = true;
wsh.AddServiceEndpoint(typeof(iService1), whb, "");
wsh.Open();
}
[System.ServiceModel.ServiceContract]
public interface iService1
{
[OperationContract,
WebGet(ResponseFormat = WebMessageFormat.Json)]
string Hello();
[OperationContract,
WebGet(ResponseFormat = WebMessageFormat.Json)]// ต้องครอบทุกตัวด้านบน
List<student> getAllstudent();
[OperationContract,
WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json,
UriTemplate="studentbyid/{StudentID}")]// ต้องครอบทุกตัวด้านบน
student getStudentById(String StudentID);
[OperationContract,
WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.WrappedRequest)]
int AddStudent(String StudentID, String FirstName, String LastName);
}
public class Service1 : iService1
{
public string Hello()
{
return "ss";
}
WCFDataContext mywcfdb1 = new WCFDataContext();
public List<student> getAllstudent()
{
return mywcfdb1.students.ToList();
}
public student getStudentById(String StudentID)
{
int sid = int.Parse(StudentID);
return mywcfdb1.students.Where(
s => s.studentId == sid).SingleOrDefault();
}
public int AddStudent(String StudentID, string FirstName, string LastName)
{
int stdID = int.Parse(StudentID);
student newstudent = new student();
newstudent.studentId = stdID;
newstudent.firstname = FirstName;
newstudent.lastname = LastName;
mywcfdb1.students.InsertOnSubmit(newstudent);
mywcfdb1.SubmitChanges();
return 1;
}
}
}
}
$(document).ready(function(){
/*
$.getJSON('http://localhost:8000/Hello?callback=?',
null,function(data){
$('#hello').val(data);
});
*/
/*
$('#submit').click(function(){
$.getJSON('http://localhost:8000/studentbyid/'+$('#studentid').val()+'?callback=?',
null,function(data){
$('#firstname').val(data.firstname);
$('#lastname').val(data.lastname);
});
*/
$('#getallstudent').click(function(){
$('#studentlist').empty();//ลบพวก table dropdown div
$.getJSON('http://localhost:8000/getAllStudent?callback=?',
null,
function(data){
$.each(data,function(i,student){
$('<tr><td>'+student.firstname+'</td><td>'+student.lastname+'</td></tr>').appendTo('#studentlist');
//alert(student.firstname);
});
}
);
});
$('#addstudent').click(function(){
$.getJSON('http://localhost:8000/addstudent?callback=?',
{
StudentID:$('#studentid').val(),
FirstName:$('#firstname').val(),
LastName:$('#lastname').val()
},
function(data){
alert('complete');
});
}
);
});

