1

嗨,我正在尝试做一个 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");

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

4

1 回答 1

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)
        {

哪个可以满足我的需要。希望这可以帮助其他人

于 2013-05-22T13:44:39.470 回答