2017-08-08 137 views
-2

我想用oracle jet登錄頁面,使用npm yoeman包管理器與grunt構建工具。從服務器中拋出 以下錯誤響應:處理錯誤:HttpRequestMethodNotSupportedException當嘗試登錄

o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: HttpRequestMethodNotSupportedException, Request method 'GET' not supported.

但我使用Ajax方法POST。

define(['ojs/ojcore','knockout', 'ojs/ojinputtext', 'ojs/ojbutton', 
'ojs/ojknockout-validation', 'ojs/ojmodel' 
], function (oj, ko) { 
/** 
* The view model for the main content view template 
*/ 
function logintestContentViewModel() { 
    var self = this; 
    self.tracker = ko.observable(); 
    self.username = ko.observable(""); 
    self.password = ko.observable(""); 
    self.clickedButton = ko.observable(); 
    self.buttonClick = function(data, event) 
    { 
     //alert("call"+JSON.stringify(data)); 
     var trackerObj = ko.utils.unwrapObservable(self.tracker); 


     alert("submittedValue====="+self.username()); 
     alert("password====="+self.password()); 
     //self.submittedValue(); 
     //change this to a valid ajax call. 
     alert("ajax call initiated"); 


     self.url = "http://192.168.0.100:8080/oauth/token?grant_type=password"; 
     self.url +="&username="; 
     self.url += self.username(); 
     self.url +="&password="; 
     self.url += self.password(); 
     //this.url += "client_id=my-trusted-client&client_secret=clientpassword"; 
     $.ajax({ 

      url: self.url, 
      type: "POST", 
      grant_type : "password", 
      data: {client_id : "my-trusted-client", client_secret: "clientpassword1"}, 
      dataType: 'json', 
      success: function(res) { 
       alert(res); 
       this.submittedValue(res.token); 
      }, 
      failure: function(jqXHR, textStatus, errorThrown) { 
       console.log(textStatus); 
       this.submittedValue("Login Failed"); 
      } 
     }) 
     //this.submittedValue(this.url) 
     return true; 

     /* 
     $.ajax({ 
      type: "POST", 
      dataType: "jsonp", 
      crossDomain:true, 
      data: "grant_type=password&username="+self.username()+"&password="+self.password()+"", 
      xhrFields: { 
       withCredentials: true 
      }, 
      beforeSend: function (xhr) { 
       xhr.setRequestHeader('Authorization', 'Basic bXktdHJ1c3RlZC1jbGllbnQ6Y2xpZW50cGFzc3dvcmQ='); 
      }, 
      url: 'http://192.168.0.100:8080/oauth/token', 
      success: function(data) { 
       alert(data); 
      } 
     });*/ 

    }; 




    self.routePage = function(data,event) 
    { 
     self.clickedButton(event.currentTarget.id); 
     return true; 
    }; 
    self.onClick = function() 
    { 
     self.buttonClick(); 
     self.routePage(); 
    } 
    self.shouldDisableCreate = function() 
    { 
     var trackerObj = ko.utils.unwrapObservable(self.tracker), 
     hasInvalidComponents = trackerObj ? trackerObj["invalidShown"] : false; 
     return hasInvalidComponents; 
    }; 
    self._showComponentValidationErrors = function (trackerObj) 
    { 
     trackerObj.showMessages(); 
     if (trackerObj.focusOnFirstInvalid()) 
     return false; 
    }; 


} 
return logintestContentViewModel; 
+0

是什麼的console.log的輸出(textStatus); ? –

+0

請提供開發人員工具控制檯的日誌 –

+0

錯誤說不支持Request方法'GET',但在您的代碼中您的ajax代碼是type:「POST」 –

回答

0

在此示例中,您將POST作爲「type」的值。嘗試將「type」更改爲「method」,看看是否有幫助。

從jQuery.ajax API文檔:

var menuId = $("ul.nav").first().attr("id"); 
var request = $.ajax({ 
    url: "script.php", 
    method: "POST", 
    data: { id : menuId }, 
    dataType: "html" 
}); 

request.done(function(msg) { 
    $("#log").html(msg); 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
}); 
0
if you wanna post and get a response use this 

var dataVal2 = {your data}; 
$.ajax({ 
type: "POST", 
url: "url", 
data: JSON.stringify(dataVal2), 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
processData: true, 
success: function (data, status, jqXHR) { 
alert(data.result); 
var projectId = data.result; 
alert(projectId); 
    }, 
error: function (xhr) { 
alert(xhr.responseText); 
} 

if you want a response by using GET method use this 

var data = {your data}; 
        $.getJSON("url", data).then(function (resData) { 
        self.DataResponse(resData); 
        }); 
and your error function alike in post method