- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 aspx 页面的 javascript 函数中获取了一个 JSON 对象。我需要从我的代码隐藏文件中获取这些数据。我该怎么做?
我的 javascript 函数是:
function codeAddress()
{
var address = document.getElementById("address").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
var requestParameter = '{' +
'output:"' + results + '"}';
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
data: requestParameter,
//contentType: "plain/text",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function() { alert("error"); }
});
});
}
Default.aspx.cs
[WebMethod]
public static string GetData( Object output)
{
return output.ToString();
}
我得到的输出是对象数组而不是实际结果 - [object Object],[object Object],[object Object]。请提供一个解决方案以获得实际结果。我正在处理的 JSON 如下所示 http://maps.googleapis.com/maps/api/geocode/json?address=M%20G%20Road&sensor=false
最佳答案
在您的 aspx 页面上创建一个 WebMethod,以获取页面上的数组:
[WebMethod]
public static GetData( type[] arr) // type could be "string myarr" or int type arrary - check this
{}
并发出json请求。
$.ajax({
type: 'POST',
url: 'Default.aspx/GetData',
data: "{'backerEntries':" + backerEntries.toString() + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(response) {}
});
或者您可以使用 .post()。
检查 ajax 请求中的 url:
GetData
WebMethod 必须在您发出 ajax 请求的页面上的 Default.aspx
上声明.
检查此问题以了解如何格式化数组以发送到网络方法。
Why doesn't jquery turn my array into a json string before sending to asp.net web method?
检查这些链接以供引用:
Handling JSON Arrays returned from ASP.NET Web Services with jQuery - 最好采取想法
How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?
关于c# - 如何将 javascript 中的 JSON 数组传递到我的代码隐藏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8430431/
我是一名优秀的程序员,十分优秀!