do I need getJson at all?

好久不见. 提交于 2019-12-13 03:18:19

问题


   $(document).ready(function ()
    {
        $(".viewmap").click(function ()
        {
            var id = $(this).attr("id");
            var responseURL = "~/changemap?id=" + id;
            //alert(responseURL);
            $.ajax(
            {
                url: responseURL,
                dataType: "json",
                type:"GET",
                success: function (dt)
                {
                    initialize(dt.Latt, dt.Longt); 
                }
            }
            );
        }
        );
    });

I use that script to make an ajax call to the page changemap.cshtml which does the following

@{

    if(!IsPost)
    {
        if(!Request.QueryString["id"].IsEmpty()&&Request.QueryString["id"].IsInt())
        {
            var countryId=Request.QueryString["id"];
            var db=Database.Open("GoogleMapView");
            var dbCmd="SELECT * FROM places WHERE id=@0";
            var row=db.QuerySingle(dbCmd,countryId);
            if(null!=row)
            {
               Json.Write(row,Response.Output);
            }
        }
    }
}

That is to return the queried data from the database in json format to the client. The Initialize function is defined as

function initialize(lat,lng)
            {
                var mapOptions = {
                    center: new google.maps.LatLng(lat,lng),zoom: 8,mapTypeId: google.maps.MapTypeId.ROADMAP 
                };
                var map = new google.maps.Map(document.getElementById("gmap"),mapOptions);
            }

But when I click the div tag of class viewmap, nothing happens. I think I miss some more script to get my application to work correctly.

I only try to implement a simple google map view in which once the user clicks a place name as a hyperlink will reload the map that matches with it.


回答1:


I think

var responseURL = "~/changemap?id=" + id;

should be

var responseURL = '@(Url.Content("~/changemap")+"?id=")' + id;



回答2:


try thr following

            success(data){

                   initialize(data.d.Latt, data.d.Longt); 



              }

for more reference as in why d is used check the following link

      http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/


来源:https://stackoverflow.com/questions/12550464/do-i-need-getjson-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!