2015-06-22 254 views
2

我已經通過所有建議的鏈接找出解決方案,但仍然當我試圖讓$.post()它發送GET作爲Request_Method,它是給我的錯誤

GET https://example.com/samewebservice/save 405 (Method Not Allowed)

它是一個跨源請求,所以我已經啓用所有CORS在服務器上, 設置如果我執行$.get()方法它給我以JSON格式響應成功。

但是,當m試圖執行$.post()方法它給我錯誤。

POST Ajax請求

function postAjax(URL,jsonData){ 

    $.post(URL,jsonData,function(data){ 
    response = data; 
    alert("In success"); 
    console.log(data); 
    },"jsonp"); 
    return response; 
} 

在瀏覽器標題我得到這個

enter image description here

回答

4

只是一個猜測。嘗試添加

function postAjax(URL,jsonData){ 
    $.post(URL,jsonData, 
    type:'POST', 
    function(data){ 
     response = data; 
     alert("In success"); 
     console.log(data); 
    }, 
    "jsonp"); 

    return response; 
} 

B/W上的JSONP數據類型可能會在這裏的原因。檢查這個answer

+0

不工作,'$ .post()'不接受'類型:'POST''作爲參數 – rachana

+0

然後,您可以更改數據類型到'json'而不是'jsonp'或將'post'函數更改爲'ajax' – Karthik

+0

我已經嘗試過兩種方法,但仍然不能正常工作 – rachana

0

你不能使用JSONP POST ...它創建一個<script>元素來獲取數據......它必須是一個GET請求。

0

我有一個[FromBody]在我的「HttpRequestMessage請求」參數之前;這是不正確解析..........