2014-03-07 1 views
0

도움말을하시기 바랍니다 작동하지 않는,하지만 그들은 도움하지 않는 것 ; http:// localhost : 53505/GetLocationData/Canada/Ontario/Toronto 그리고 정확히 예상 한대로 - 내 AddressController의 결과.MVC 5 경로는 나는이에 대한 몇 가지 게시물을 본 적이 ...

... 이제

, 내 응용 프로그램에서, 내 ClientController는 클라이언트/인덱스에 대한 호출을 통해보기 ~/조회/클라이언트/Index.cshtml

을 시작보기 것을

좋은 GEO-IP 서비스에서 결과를 얻은 후에 위에서 언급 한 것과 같은 AddressController 함수를 비동기 적으로 호출하려고하는 .ajax 자바 스크립트. $(document).ready(function() {

var urlGeoIeoip = "http:// smart-ip . net/geoip-json?callback=?"; 

    $.ajax({ 
     url: urlGeoIeoip, 
     type: "GET", 
     dataType: "json", 
     timeout: 2000, 
     success: function (geoipdata) { 

     $("#AddressCity").data("kendoComboBox").value(geoipdata.city); 
     $("#AddressState").data("kendoComboBox").value(geoipdata.region); 
     $("#AddressCountry").data("kendoComboBox").value(geoipdata.countryName); 

     var $form = $(this); 

     $.ajax({ 
      url: "getlocationdata", 
      type: "GET", 
      data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
      timeout: 500, 
      success: function(data) { 
       var $target = $($form.attr("data-htci-target")); 
       var $newHtml = $(data); 
       $target.replaceWith($newHtml); 
      } 
     }); 


     } 
    }).fail(function(xhr, status) { 
    if (status === "timeout") { 
     // log timeout here 
     } 
    }); 
}); 

작동하지만 주소 컨트롤러가 아닌 ClientController가 요청에 응답합니다 !!

Request.Url 인/Client/Index가 다음과 같이 표시됩니다. http:// localhost : 53505/Client/Index/GetLocationData?country=Canada&region=Ontario&city=Toronto 왜 내 AddressController에 도달하지 않습니까?

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); 

     routes.MapRoute(
      name: "GetLocationData", 
      url: "getlocationdata/{country}/{region}/{city}", 
      defaults: new { controller = "Address", action = "GetLocationData"} 
     ); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

답변

1

당신이 $ 아약스 내부의 URL을 변경하는 경우에 (대신 "getlocationdata"의) "/ getlocationdata"여기

내 RouteConfig입니까?

+0

는 그것을 시도 - 아약스 호출 어딘가에 공간으로 꺼지고 블랙홀에 빨려됩니다. 컨트롤러는이 웹 응용 프로그램에 대한 ajax 호출을 수신하지 않습니다. 나는 그것이 어디에서 일어 났는지 확실하지 않다. –

+0

이제는 작동하는 것 같다 - 아마도 캐시 된 것이 있었을 것인가? 지금 모두 좋다. –

0
당신이 (데이터 옵션을 통해 매개 변수를 전달) 쿼리 문자열을 통해 데이터를 전송하려고하는 것 같습니다

:이 같은 경로를 정의해야합니다이 때문에

$.ajax({ 
     url: "getlocationdata", 
     type: "GET", 
     data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

옵션 1 을 :

routes.MapRoute(
     name: "GetLocationData", 
     url: "getlocationdata", 
     defaults: new { controller = "Address", action = "GetLocationData"} 
    ); 

이 경로를 수신합니다 http://localhost:53505/GetLocationData?country=Canada&region=Ontario&city=Toronto

그럼 당신은 (cshtml 아래 가정)보기에 URL을 얻을 수있는 UrlHelper를 사용할 수 있습니다

$.ajax({ 
     url: "@Url.RouteUrl("GetLocationData")", //Notice this is the Name of the Route 
     type: "GET", 
     data: { 'country': geoipdata.countryName, 'region': geoipdata.region, 'city': geoipdata.city }, 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

이 당신이 자유롭게 의견을 수정하지 않고도 경로 설정의 경로를 변경할 수 있습니다.

옵션 2 당신이처럼 (inurl 데이터를 사용하여) 경로를 유지하려면,이처럼 Ajax 호출을 정의해야합니다 :

$.ajax({ 
     url: "/getlocationdata/" + geoipdata.countryName + "/" + geoipdata.region + "/" + geoipdata.city, //This will manually build the route as you defined it 
     type: "GET", 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 

당신은 여전히위한 UrlHelper을 사용할 수 있습니다 이 경로의 유형,하지만 당신은 Javascript의에서 쉽게 사용할 수 없습니다

$.ajax({ 
     url: "@Url.RouteUrl("GetLocationData", new{country = "SomeCountry",region="SomeRegion",city="SomeCity"})", //unfortunately, you need these while you are in C# code 
     type: "GET", 
     timeout: 500, 
     success: function(data) { 
      var $target = $($form.attr("data-htci-target")); 
      var $newHtml = $(data); 
      $target.replaceWith($newHtml); 
     } 
    }); 
+0

노트 주셔서 감사합니다. 다른 문제를 해결했습니다. –

관련 문제