2016-06-22 6 views
2

問題は、以下の方法を使用しているときにURL全体が表示されていることです。たぶん、hrefのGET要求のためです。私はこれを避けたいと思うし、私はPOST要求をしたい。パラメータを渡すとhref(アンカータグ)リクエストがGETの代わりにPOSTを要求する

index.htmlを

<a id="ccviewMiniStatementId">View mini statement</a> 

nonFinancial.js

$('#ccviewMiniStatementId').click(function(event) { 

    var ccMonthVal=$("#ccMonthId").val(); 
    var ccYearVal=$("#ccYearId").val(); 
    var cCRefNumId=$("#cCRefNumId").val(); 

    var errExpVal = validateCcExpiry(); 

    if (errExpVal==false) 
    { 
     event.preventDefault(); 
     return false; 
    } 

    // I am passing values to my controller and there it is GET 
    //because POST is not supported. I want to make it POST. 
    $("#ccviewMiniStatementId").attr("href", "getpdfcreditcard?month="+ccMonthVal+"&year="+ccYearVal+"&cCReferenceNo="+cCRefNumId); 
    $("#pdfViewer").submit();   
}); 

マイコントローラNonFinancial.java

@RequestMapping(value="/getpdfcreditcard", method=RequestMethod.GET, params = {"month","year","cCReferenceNo"}) 
public ResponseEntity<byte[]> getPDF(@RequestParam(value="month", required = true) String month, 
    @RequestParam(value="year", required = true) String year, 
    @RequestParam(value="cCReferenceNo", required = true) String cCReferenceNo, 
    HttpServletResponse response) throws IOException, SQLException { 

    logger.debug("month/year/cCReferenceNo: "+month+"/"+year+"/"+cCReferenceNo); 
} 

だから私は望む:私はアンカータグをクリックしているときには、POSTを行う必要があります。それ、どうやったら出来るの?私はあなたがjQueryのを使用しているよう<form></form>

答えて

0

が見えます使用したくないので、それは簡単です:HTMLでhref利用POSTを作成することはできません

$('a').click(function() { 
    var fullLink = $(this).attr('href'); 
    var splittedLink = fullLink.split("?"); 
    var href = splittedLink[0]; 
    var qs = splittedLink[1]; 

    // convert qs to body 
    var splittedQs = qs.split('&'); 
    var body = {}; 

    for(var key in splittedQs) { 
     var t = splittedQs[key].split('='); 
     body[t[0]] = t[1]; 
    } 

    $.ajax({ 
     type: 'post', 
     data: body, 
     url: href, 
     success: function() { ... } 
    }); 

    return false; // prevent default 
}); 
+0

私がajaxを使用すると、pdfはcontoller thatsから生成されません。なぜ私はajaxを使用していません。 –

+0

@RahulKhanna Ajaxを必要としない場合は、フォームを動的に生成して送信することができます。 – modernator

0

。しかし、@modernatorが指摘するように、JavaScriptを使用してクリックイベントを聞き、AJAXリクエストを手動でトリガーすることができます。

また、入力が隠されたフォームを使用することもできます。ここにはsubmitメソッドを指定することができます。例:

<form method="post" action="<your-url>"> 
    <input type="hidden" name="month" value="<month-value>"> 
    <button type="submit">View Statement</button> 
</form> 
+0

しかし、私は価値も渡したいと思います。どうすればいい? –

+0

'type =" hidden "入力を使用します。上の私の例では、フォームに '{month:" "}' – jens1101

+0

のようなものを提出します。 –

関連する問題