I used ruby on rails to make a RESTful api, and also use a gatekeeper for authentication and authorization. As you know, the gatekeeper generates some OAuth2 api, and the two that I need to use are / users, which are a mail request and / oauth / token, which make a token for me. Api, which I create, which publish, process, put works in the postmaster and android studio and web browser. But the post api / users and / oauth / token, which are generated by the gatekeeper, do not work in the web browser, but work fine in the android andmail studio. It confused me. The error I accept when calling this api is 404, I checked the ruby mining log on the server and it says that there is no route matching. The line is that the method type and routes are correct.
This is the code I used in the reaction. I used axios:
var url="http://x.x.x.x/oauth/token";
axios.post(url,{
"username":"1",
"password":"password",
"grant_type":"password"
},{headers:{"Content-Type":"application/json"}}).then( (response) => {
console.log(response.data);
})
.catch( (error) => {
console.log(JSON.stringify(error));
});
And also use raw jQuery to query and get the same error. all my api work fine with two api:
var firstname = document.forms["registerForm"]["first_name"].value;
var lastname = document.forms["registerForm"]["last_name"].value;
var pass = document.forms["registerForm"]["password"].value;
var passconfirm = document.forms["registerForm"]["password_confirmation"].value;
var json_data = {
"async": true,
"crossDomain": true,
"url": send,
"method": "POST",
"headers": {
"content-type": "application/json",
},
"processData": false,
"data":
{
"user": {
"user_name": username,
"password": pass,
"password_confirmation": passconfirm,
"user_type": "admin"
},
"profile": {
"first_name": firstname,
"last_name": lastname
}
}
}
$.ajax(json_data).done(function (response) {
console.log(response);
});
console.log(json_data['data']);
console.log(username);
the output of this code is console.log (JSON.stringify (error)); this:
{"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"},"method":"post","url":"http://x.x.x.x/oauth/token.json","data":"{\"username\":\"1\",\"password\":\"password\",\"grant_type\":\"password\"}"},"request":{}}
I found the request header and response header in the browser:
Response Header:
Content-Type
application/json; charset=UTF-8
Content-Length
34
Connection
keep-alive
Status
404 Not Found
X-Request-Id
d593b73f-eec8-41cd-95cd-e4459663358c
X-Runtime
0.002108
Date
Mon, 13 Nov 2017 11:19:26 GMT
X-Powered-By
Phusion Passenger 5.1.11
Server
nginx/1.12.1 + Phusion Passenger 5.1.11
Request headers (427 B)
Host
x.x.x.x
User-Agent
Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/56.0
Accept
text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Language
en-US,en;q=0.5
Accept-Encoding
gzip, deflate
Access-Control-Request-Method
POST
Access-Control-Request-Headers
Origin
http://localhost:3000
Connection
keep-alive