<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AJAX Remote Test</title>
<meta name="description" content="" />
<meta name="author" content="小J" />
<script src="./lib/jquery-1.7.2.min.js"></script>
<script>
jQuery.support.cors = true;
$(document).ready(function(){
$("#callBtn").click(function (event) {
helloWorld();
});
$('#showDataBtn').click(function (event){
alert(getData());
});
});
function helloWorld(){
callWebService("GetMenuList", onServiceResult, onServiceFault);
}
function callWebService(serviceMethod, onSuccess, onFault){
var service = {};
//請求格式,如GET , POST
service.type = "POST";
//Remote Server URL and API Name
service.url = "http://localhost:9689/WebService1.asmx/GetSourceUT";
service.cache = false;
//service.contentType = "application/x-www-form-urlencoded";
//service.contentType = "text/plain; charset=utf-8";
//service.contentType = "text/xml; charset=utf-8";
//service.contentType = "application/xml; charset=utf-8";
service.contentType = "application/json; charset=utf-8";
service.data = getRequestData();
service.dataType = "json";
//service.dataType = "text";
//service.dataType = "xml";
service.success = onSuccess; //Remote 成功時,會呼叫的function
service.error = onFault; //Remote 失敗時,會呼叫的function
//發送
$.ajax(service);
}
function getRequestData()
{
var obj = {};
var shell = [];
var contentsA = [0,1,0,2,3];
var contentsB = ['a',0,'b',{key:'op'}];
shell[0] = contentsA;
shell[1] = contentsB;
// API 可以用的參數
obj.ResultCode = '88A5E59C-C9F2-492A-B77F-2A27F90CBBA9';
obj.ResultList = shell;
//轉換成 JSON 字串
return JSON.stringify(obj);
}
function onServiceResult(data, textStatus, jqXHR){
alert('成功');
}
function onServiceFault(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
</script>
</head>
<body>
<div>
<input type="button" id="callBtn" value="呼叫API" />
<input type="button" id="showDataBtn" value="show data format" />
</div>
</body>
</html>
ASP.Net Code WebApplication1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebApplication1.DataSet1TableAdapters;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public List<UserInfo> GetSourceUT(Guid ResultCode, List<Object> ResultList)
{
var memnuList = getUserList();
for (var i = 0; i < memnuList.Count; i++)
{
memnuList[i].List = getUserList();
}
memnuList[0].ResultCode = ResultCode;
memnuList[0].ResultList = ResultList;
return memnuList;
}
private List<UserInfo> getUserList()
{
var menuList = new List<UserInfo>();
var names = new List<String> { "Jeff", "Aelx", "Olivia", "Emily" };
var tel = new List<String> { "0911-234138", "0955-555555", "0966-663662", "0912-123456" };
var age = new List<Byte> { 20, 38, 32, 30 };
for (var i = 0; i < names.Count; i++)
{
menuList.Add(getUser(names[i], tel[i], age[i]));
}
return menuList;
}
private UserInfo getUser(String userName , String tel , Byte age)
{
var user = new UserInfo();
user.UserID = Guid.NewGuid();
user.UserName = userName;
user.TEL = tel;
user.Age = age;
return user;
}
}
}
ASP.Net Code UserInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class UserInfo
{
public Guid UserID { get; set; }
public string UserName { get; set; }
public string TEL { get; set; }
public int Age { get; set; }
public List<UserInfo> List { get; set; }
//測試參數
public Guid ResultCode { get; set; }
public List<Object> ResultList { get; set; }
}
}
沒有留言:
張貼留言