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 が有効であることがわかります。

ウィジェット コントローラーのコードは次のとおりです。

 [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