Skip to main content
 首页 » 编程设计

c#之从客户端向服务器端发送数据之无效的 JSON PRIMITIVE : location

2024年02月23日16wuhuacong

目前,我有一个用户在 ajax bing map 上创建了很多点。当用户按下“提交”按钮时,我想获取存储在 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); 
                } 
            }); 
} 

请您参考如下方法:

尝试添加数据类型:

dataType: "json", 

此外,我认为您的数据应该是“s”而不是“xmlLoc”,因为这是您在操作中要查找的内容。

然后尝试改变你的 Action:

[HttpPost] 
public static string saveLocations(string s) 
{ 
    return s; //just for testing purposes 
}