2017-11-17 5 views
3

다음 ajax 스크립트를 사용합니다.ASP .NET 웹 API에 아약스 GET 요청 보내기

$.ajax({ 
      dataType: 'json', 
      url: url, 
      data: tuDispId, 
      type: "GET", 
      success: function (data) { 
       bindData(data); 
       $("#alert-placeholder").empty(); 
       $('#alert-placeholder').removeClass('alert alert-danger'); 
      }, 
      error: function (xhr, textStatus, errorThrown) { 
       $('#alert-placeholder').addClass('alert alert-danger'); 
       $('#alert-placeholder').html(errorThrown); 
      } 
     }); 

속성은 웹 API에서 메소드 이전에 라우팅하십시오.

[Route("api/tudisp/Edit/{tuDispId}")] 
     public IHttpActionResult Edit(int tuDispId) 
{ 
} 

아약스의 genarated 요청.

http://localhost:xxxxx/api/tudisp/Edit/?179 

강제로 ajax가 '?'기호를 생성하지 않게하는 방법. by id 매개 변수. 그 어떤 응용 프로그램의 경우

+1

방법 당신은 귀하의 요청에 따라이 같은'data' 매개 변수를 작성해야 –

+0

을 GET이기 때문에'데이터 : {ID : tuDispId}' –

+0

'URL : URL + "/"+ tuDispId,' – mplungjan

답변

4

는 아약스 옵션의 url 속성을 변경하는 가장 간단한 방법 ...

$.ajax({ 
    dataType: 'json', 
    url: "http://localhost:xxxxx/api/tudisp/Edit/" + tuDispId, 
    type: "GET", 
    success: function (data) { 
     bindData(data); 
     $("#alert-placeholder").empty(); 
     $('#alert-placeholder').removeClass('alert alert-danger'); 
    }, 
    error: function (xhr, textStatus, errorThrown) { 
     $('#alert-placeholder').addClass('alert alert-danger'); 
     $('#alert-placeholder').html(errorThrown); 
    } 
}); 

GET 매개 변수가 자동으로 잘하는 쿼리 문자열로 URL에 추가된다 예상하고 있지만 라우팅을 그대로 사용하는 것은 아닙니다.

그러나 기존 Ajax 요청을 수정하려면 사전 필터링을 사용할 수 있습니다. 이 예는 다음 난 강력하게 약간의 시간이 만드는 지출 권하고 싶습니다 같은 것을 사용하고 싶다면

$.ajaxPrefilter(function(options, originalOptions, jqXHR) { 
    options.data = ""; // this removes the querystring 
    for (key in originalOptions.data) { 
     options.url = options.url.replace("{" + key + "}", originalOptions.data[key]); 
    } 
}); 

$.ajax({ 
    url: "http://localhost:xxxxx/api/tudisp/Edit/{tuDispId}", 
    data: { 
     "tuDispId": 179 
    } 
}); 

... 데이터 개체에서 지정된 값 {variable} 교체, 아약스 호출의 URL을 수정 더 강력하지만, 원하는 것을 할 수있는 방법의 예입니다.

+0

내가 가지고있는이 솔루션을 이미 테스트를 마쳤습니다. 섹션 데이터에서 wheather가 가능할 지 궁금합니다. 고마워요 – maciejka

+1

거기 가서 - 당신이 찾고있는 뭔가를 추가했습니다. 나는 그것을 실제로 좋아하고 앞으로 나 자신을 사용할지도 모른다 : D – Archer

관련 문제