【发布时间】:2012-04-13 19:37:14
【问题描述】:
目前,我有一个用户正在 ajax bing 地图上创建很多点。当用户按下“提交”按钮时,我想获取存储在 map.entities 中的所有位置,我构建了一个 xml 格式的字符串,我想将它保存在我的数据库中:
<locations> <location><lat><lon>1.234</lon></lat></location>.....</locations>
,所以在构建我的 XML 字符串之后,这不是问题,我将它存储在可变位置并执行:
$. ajax({
type: "POST",
url: "myPage.aspx/saveLocations"
data: {"xmlLoc: x"},
async: true,
cache: false,
success: alert ("success" + msg)
error: ....
但不幸的是,这似乎不是传递我的数据的方式。这是我唯一一次成功,但味精是未定义的!!!
如果我写 data: x,
我的服务器端代码:
[Web Method]
public static string saveLocations(string s)
{
return s; //just for testing purposes
}
我不确定如果我必须使用 json 或其他东西,我是一个初学者,所以我不知道从哪里开始!非常感谢
编辑:我正在尝试另一种解决方法,但我总是收到无效的 json 原始错误!!!
var locations = '{ "location" : [';
function createBoundary() {
for (x = 0; x < map.entities.getLength(); x++) {
var pin = map.entities.get(x);
locations += '{ "latitude": "' + pin.getLocation().latitude +'", "longitude": "' + pin.getLocation().longitude + '"},';
}
locations += ']}';
jQuery.ajax({
type: "POST",
url: "Profiles_Schedules.aspx/GetXmlLoc",
data: eval("(" + locations + ")"),
contentType: "application/json;charset=utf-8",
datatype: "json",
async: true,
cache: false,
success: function (msg) {
alert("Success " + msg.d);
},
error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);
}
});
}
【问题讨论】:
-
如果将数据设置为数据会发生什么:{"s: 'x'"}
-
你不能这样写,我得到了一个意外的令牌}