使用jQuery从asp.net响应中重新获取JSON值

时间:2022-10-10 16:38:00

I'm working on a small project. Mostly to gain some understanding because I am a complete web noob. Which may or may not be apparent in the following question(s).

我正在做一个小项目。主要是为了获得一些理解,因为我是一个完整的网络菜鸟。在以下问题中可能会或可能不会显而易见。

My end result is fetching a bunch of values from a database. Putting them into a JSON object and then using some jQuery/JavaScript to work with the data. I've been doing a ton of reading, been through tons of posts on here but I can't find something as it relates to my problem.

我的最终结果是从数据库中获取一堆值。将它们放入JSON对象,然后使用一些jQuery / JavaScript来处理数据。我一直在做大量的阅读,在这里通过大量的帖子,但我找不到与我的问题有关的东西。

I'm working in VS2010 C#. I'm bound by IE7 on the client side, but I can test with Firefox. I'm also working on localHost for now.

我在VS2010 C#工作。我受客户端IE7的约束,但我可以用Firefox测试。我现在也在为localHost工作。

My JavaScript is as follows.

我的JavaScript如下。

function plotMarkers() {
var pathStart = new String(window.location);
var newPath = pathStart.replace("Default.aspx", "getMarkers.aspx");
alert(newPath);
$("#statusDiv").html('<a href="' + newPath + '">JSON Data</a>');

$.getJSON(newPath, function (data) {

    alert("Loading JSON");
    $.each(data, function (index, elem) {

        var myLatlng = new google.maps.LatLng(elem.lat, elem.lng);
        var marker = new gogle.maps.Marker({
            position: myLatlng,
            title: elem.title
        });

        marker.setMap(map);
    });

}, function () { alert("loaded") });}

I don't get any Javascript errors in Firefox, but I do in IE7. The alert("Loading JSON") never fires anywhere. I've never seen it.

我在Firefox中没有得到任何Javascript错误,但我在IE7中做错了。警报(“正在加载JSON”)从不会在任何地方触发。我从来没有见过它。

My getMarkers.aspx code

我的getMarkers.aspx代码

public partial class getMarkers : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        JavaScriptSerializer jsonSer = new JavaScriptSerializer();

        List<marker> markerList = new List<marker>();
        markerList.Add(new marker(-34.397, 150.644, "Test"));
        markerList.Add(new marker(-50, 150.644, "Test2"));

        string sJSON = jsonSer.Serialize(markerList);
        Response.ContentType = "application/json";
        Response.Write(sJSON);
    }
}

I can click the link in "statusDiv" and it takes me to the aspx page. I get an error in IE7 but it works fine in Firefox. The JSON that is parsed back to me in those pages is correct. I copy and pasted the response in another function and the plotter put the two pins on my map.

我可以单击“statusDiv”中的链接,它将我带到aspx页面。我在IE7中遇到错误,但在Firefox中运行正常。在这些页面中解析回来的JSON是正确的。我在另一个函数中复制并粘贴了响应,绘图仪将两个引脚放在我的地图上。

  1. The code does not seem to enter the $.getJSON function.

    代码似乎没有进入$ .getJSON函数。

  2. There is a problem with IE7 (or a problem in general) with how my getMarkers.aspx is set up. Googled some tutorials and this is where I ended up. I might be completely wrong in my approach. Or I missed something I was supposed to do. I can't seem to find that specific tutorial, it must have been closed on accident amidst my furious 2 day and counting Google adventure.

    IE7(或一般问题)与我的getMarkers.aspx设置方式存在问题。谷歌搜索一些教程,这是我最终的地方。我的方法可能完全错了。或者我错过了我应该做的事情。我似乎无法找到那个具体的教程,它必须在我激烈的第二天和谷歌冒险中意外关闭。

Firefox getMarkers.aspx displays the JSON as the first line followed by the html. Just plaintext in the browser.

Firefox getMarkers.aspx将JSON显示为第一行,后跟html。只是浏览器中的纯文本。

IE7 displays an XML error and tells me the JSON string isn't allowed at the top level of the document. I did read some articles about IE7 always trying to parse XML with an elaborate workaround. Which I can't do, because I don't know how many clients will be using it. Is there a better way to do what I'm doing?

IE7显示XML错误,并告诉我文档顶层不允许使用JSON字符串。我确实阅读了一些关于IE7的文章,总是试图通过精心设计的解决方法来解析XML。我不能这样做,因为我不知道有多少客户会使用它。有没有更好的方法来做我正在做的事情?

If anyone could point me in the direction I need to go I would greatly appreciate it. Hopefully my post isn't too convoluted.

如果有人能指出我需要走的方向,我将非常感激。希望我的帖子不会太复杂。

3 个解决方案

#1


1  

You probably want a Response.End() at the end of your page load method, so that the rest of the web page isn't rendered after your JSON.

您可能需要在页面加载方法结束时使用Response.End(),以便在您的JSON之后不呈现网页的其余部分。

#2


0  

It looks to me, for starters, that you need the json2 library to handle the issues with IE7.

对于初学者来说,我认为你需要json2库来处理IE7的问题。

As for firing your function - I can't see anywhere where plotMarkers() is actually called, it's only defined?

至于触发你的函数 - 我看不到实际调用plotMarkers()的地方,它只是定义了吗?

#3


0  

Are you trying to display markers on a map in an existing page when the #statusDiv link is clicked ? If so, you're doing it wrong : dont put a link in #statusDiv, just make sure the getJSON function is called when you click on it. try this :

您是否在单击#statusDiv链接时尝试在现有页面的地图上显示标记?如果是这样,你做错了:不要在#statusDiv中放一个链接,只需确保点击它时调用getJSON函数。尝试这个 :

function plotMarkers() {
    var pathStart = new String(window.location);
    var newPath = pathStart.replace("Default.aspx", "getMarkers.aspx");
    alert(newPath);
    $("#statusDiv").text('JSON Data');
    $("#statusDiv").on('click',function(){

        $.getJSON(newPath, function (data) {

        alert("Loading JSON");
        $.each(data, function (index, elem) {

            var myLatlng = new google.maps.LatLng(elem.lat, elem.lng);
            var marker = new gogle.maps.Marker({
                position: myLatlng,
                 title: elem.title
            });

            marker.setMap(map);
        });

    }, function () { alert("loaded") });
});
}

#1


1  

You probably want a Response.End() at the end of your page load method, so that the rest of the web page isn't rendered after your JSON.

您可能需要在页面加载方法结束时使用Response.End(),以便在您的JSON之后不呈现网页的其余部分。

#2


0  

It looks to me, for starters, that you need the json2 library to handle the issues with IE7.

对于初学者来说,我认为你需要json2库来处理IE7的问题。

As for firing your function - I can't see anywhere where plotMarkers() is actually called, it's only defined?

至于触发你的函数 - 我看不到实际调用plotMarkers()的地方,它只是定义了吗?

#3


0  

Are you trying to display markers on a map in an existing page when the #statusDiv link is clicked ? If so, you're doing it wrong : dont put a link in #statusDiv, just make sure the getJSON function is called when you click on it. try this :

您是否在单击#statusDiv链接时尝试在现有页面的地图上显示标记?如果是这样,你做错了:不要在#statusDiv中放一个链接,只需确保点击它时调用getJSON函数。尝试这个 :

function plotMarkers() {
    var pathStart = new String(window.location);
    var newPath = pathStart.replace("Default.aspx", "getMarkers.aspx");
    alert(newPath);
    $("#statusDiv").text('JSON Data');
    $("#statusDiv").on('click',function(){

        $.getJSON(newPath, function (data) {

        alert("Loading JSON");
        $.each(data, function (index, elem) {

            var myLatlng = new google.maps.LatLng(elem.lat, elem.lng);
            var marker = new gogle.maps.Marker({
                position: myLatlng,
                 title: elem.title
            });

            marker.setMap(map);
        });

    }, function () { alert("loaded") });
});
}