gpt4 book ai didi

c# - 如何将 javascript 中的 JSON 数组传递到我的代码隐藏文件

转载 作者:行者123 更新时间:2023-11-30 13:26:02 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com