Jquery getJSON在MVC3中不起作用

时间:2013-05-22 13:29:04

标签: jquery asp.net json asp.net-mvc-3

您好我正在尝试使用getJSON,但由于某种原因,它会默默地失败。

以下是代码:

   $("#distanceMiles").change(function () {

                        $("#distanceMiles option:selected").each(function () {

                             var manufacturerId = <%= Model.Manufacturer.Id%>;

                             var postcodeEntered = $("#enterPostCode").val();

                             var milesEntered = $(this).val();


                               if (postcodeEntered != null && milesEntered != null) {

                                   var fqdn = "<%: Model.FullyQualifiedDomainName %>";

                                   var theUrl ="http://localhost:7310/Widgets/GetPostcodes/" + manufacturerId + "/" + postcodeEntered + "/" + milesEntered;
                                   alert(theUrl);

                                   // 3. Get the response back from the Controller (which will arrive in the form of a callback
                                   $.getJSON(theUrl + "?callback=?", { }, callback);
                                   alert("here");
                                   // 4. This callback will then decide ? Do I call myself and ask again, OR do i just return (unwind the stack) 
                                   // This will get called AFTER the json has finished i.e. my controller returns

                                   function callback(data1) {

                                       // This will be filled in once i am receiving data back...

                                       alert(data1);


                                   }
                               }

                        });
                    });

我正在使用MVC3并检查了以下内容: 在glabal.asax中正确设置了路由:

routes.MapRoute("GetPostcodes", "Widgets/GetPostcodes/{manufacturerId}/{postcodeEntered}/{milesEntered}/{callback}", new { controller = "Widgets", action = "GetPostcodes", manufacturerId = 0, postcodeEntered = "", milesEntered = 0, callback = "" });

参数都有值:

 alert(theUrl);

如果我将Url放在地址栏中,它会按预期点击我的WidgetController并返回,所以我知道url是有效的。

这是Widget控制器代码:

 [JsonpFilter]
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetPostcodes(int manufacturerId, string postcodeEntered, int milesEntered, String callback)
        {
            //get long and lat for entered postcode
            var postcodeData = _postcodeRepository.GetPostcodeFromCode(postcodeEntered);

            var postLong = postcodeData.Longitude;

            var postLat = postcodeData.Latitude;

            //Using the manufacturerofflineretailers get all the stores which has the postcodes
            var listRetailers =
                _manufacturerOfflineRetailerRepository.GetManufacturerOfflineRetailerForManufacturer(manufacturerId);


            //we need to add a list of stores to display
            var anonymous2 = new List<StoreJson>(); // a list of my anonymous type without the relationships

            //then we want to loop through every postcode using calcDistance with  entered postcode long and lat and each store long and lat and only add if less than milesentered.
            foreach (var retailer in listRetailers)
            {
                var listStores = _storeRepository.GetAllStoresForRetailer(retailer.RetailerId);

                foreach (var store in listStores)
                {


                    //get lat long using store postcodeid
                    var storeData = _postcodeRepository.GetPostcode(store.PostcodeId);

                    var retailerData = _retailerRepository.GetRetailer(store.RetailerId);

                    var storeName = retailerData.Description;

                    var address1 = store.Address1;

                    var townCity = store.TownCity;

                    var postcode = store.Postcode;

                    var telephone = store.Telephone;

                    var fax = store.Fax;

                    var storeLong = storeData.Longitude;
                    var storeLat = storeData.Latitude;
                    var calcDistance = GeoCodeCalc.CalcDistance(postLong, postLat, storeLong, storeLat);

                    // Create the reply for the client to consume
                    var storeJson = new StoreJson
                                        {
                                            StoreName = storeName,
                                            Address1 = address1,
                                            TownCity = townCity,
                                            Postcode = postcode,
                                            Telephone = telephone,
                                            Fax = fax,
                                            Distance = calcDistance
                                        };

                    //we only want to add this if the calcDistance is less than milesEntered
                    if (calcDistance <= milesEntered)
                    {
                        anonymous2.Add(storeJson);
                    }
                }

            }

            return Json(anonymous2, JsonRequestBehavior.AllowGet);

        }

我也看过小提琴手,但它没有尝试做任何事情,所以没有发生错误,但它没有到达我的下一个警报:

 $.getJSON(theUrl + "?callback=?", { }, callback);
                                   alert("here");

任何帮助表示赞赏 - 不确定为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

确定为什么getJSON不起作用,并且仍然想要一些见解,如果有人除了让我的代码工作,我使用ajax如下:

                           var theUrl ="/Widgets/GetPostcodes/" + manufacturerId + "/" + postcodeEntered + "/" + milesEntered;


                             $.ajax({
                                type: "POST",
                                //contentType: "application/json; charset=utf-8",
                                url: theUrl,
                                data: { 'manufacturerId': manufacturerId, 'postcodeEntered': postcodeEntered, 'milesEntered': milesEntered },
                                dataType: "json",
                                success: function (data) {

                                    alert(data);
                                }
               });  

并将控制器更改为:

[JsonpFilter]
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetPostcodes(int manufacturerId, string postcodeEntered, int milesEntered, String callback)
        {

我需要它做什么。希望这可以帮助别人