[問題]WebService與Json的問題\
各位先進們好
小弟最近在嘗試用ajax與webservice來做互動
但在過程中 只要是碰到要傳遞json格式的資料
就會一直出現 [object Object] 500 Internal Server Error
在網路上也找了好多方法 但怎麼試還是出現上面的錯誤
以下是我的寫法 還請各位指導那些地方寫錯需要改進的
webservice部分我用了JSON.NET提供的方法以及內建的JavaScriptSerializer
javascript:
$(document).ready(function () {
$("#bt1").click(function () {Json()});
});
function Json()
{
var person ={req:{age: "10",name: "小傑",id: "TT-541"}};
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(person),
url: 'ForAJAX.asmx/GetJson',
success: function (data) {
var tmp = JSON.parse(data.d);
$("#content").text(tmp[0].name+"--"+tmp[0].id+"--"+tmp[0].age);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr + "\n" + xhr.status + "\n" + thrownError);
}
});
}
WebService:
using System.Web.Script.Serialization;
using Newtonsoft.Json;
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetJson(string req)
{
var mm = JsonConvert.DeserializeObject<Member>(req);
Member[] mem1 = new Member[]{new Member(){age=mm.age,id=mm.id,name=mm.name}};
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(mem1);
}
public class Member
{
public string name;
public string id;
public string age;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.137.153.198
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1488386168.A.B09.html
推
03/02 00:37, , 1F
03/02 00:37, 1F
→
03/02 00:44, , 2F
03/02 00:44, 2F
→
03/02 00:46, , 3F
03/02 00:46, 3F
→
03/02 00:46, , 4F
03/02 00:46, 4F
推
03/02 01:06, , 5F
03/02 01:06, 5F
→
03/02 01:06, , 6F
03/02 01:06, 6F
→
03/02 01:06, , 7F
03/02 01:06, 7F
→
03/02 07:18, , 8F
03/02 07:18, 8F
推
03/02 09:51, , 9F
03/02 09:51, 9F
→
03/02 09:59, , 10F
03/02 09:59, 10F
推
03/02 11:28, , 11F
03/02 11:28, 11F
→
03/02 11:30, , 12F
03/02 11:30, 12F
→
03/02 11:30, , 13F
03/02 11:30, 13F
→
03/02 13:15, , 14F
03/02 13:15, 14F
→
03/02 19:42, , 15F
03/02 19:42, 15F
→
03/02 19:43, , 16F
03/02 19:43, 16F
→
03/02 19:43, , 17F
03/02 19:43, 17F
→
03/02 19:43, , 18F
03/02 19:43, 18F
→
03/02 19:43, , 19F
03/02 19:43, 19F
→
03/02 19:43, , 20F
03/02 19:43, 20F
→
03/02 19:44, , 21F
03/02 19:44, 21F