Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery getJSON在MVC3中不起作用_Jquery_Asp.net_Json_Asp.net Mvc 3 - Fatal编程技术网

Jquery getJSON在MVC3中不起作用

Jquery getJSON在MVC3中不起作用,jquery,asp.net,json,asp.net-mvc-3,Jquery,Asp.net,Json,Asp.net Mvc 3,嗨,我正在尝试做一个getJSON,但由于某种原因,它正在默默地失败 代码如下: $("#distanceMiles").change(function () { $("#distanceMiles option:selected").each(function () { var manufacturerId = <%= Model.Manufacturer.Id%>;

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


                                   }
                               }

                        });
                    });
所有参数都具有以下值:

 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为什么不起作用,如果有人有任何帮助,我仍然希望获得一些见解,但为了使我的代码正常工作,我使用了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)
        {
这正是我需要它做的。希望这能帮助别人

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