I'm trying to do and understand xmlhttprequest via API
I'm trying to figure out why my api doesn't seem to be receiving my javascript post parameters
I have modules/logins/assets/api.json:
{
"Login": {
"url_segments": "logins/modal_login/",
"request_type": "POST",
"description": "Fetch rows from table for uname and password",
"enableParams": true,
"required_fields": [
{
"name": "username",
"label": "Username"
},
{
"name": "password",
"label": "Password"
},
{
"name": "remember",
"label": "Remember"
}
],
"authorization": "*"
}
}
// in the above I tried the url like this as well:
"url_segments": "logins/modal_login/{username}/{password}/{remember}",
-----------------------------------
here is the click event set on, mywebsite.js that my browser shows sending the username, password and remember params:
function submitLogin() {
...
var params = {
username,
password,
remember
}
var targetUrl = baseUrl + 'logins/modal_signin/';
const http = new XMLHttpRequest();
http.open('POST', targetUrl);
http.setRequestHeader('Content-type', 'application/json');
http.setRequestHeader('trongateToken', token); // security/login -- NOT csrf -- ?not needed?
http.send(JSON.stringify(params));
http.onload = function() {
if (http.status == 401) {
//invalid token! -- redirect to...
window.location.href = baseUrl + 'posts/all';
} else if(http.status == 200) {
var results = JSON.parse(http.responseText);
var p_node = document.createElement("p");
p_node.innerHTML = (typeof results == 'string') ? $results: results[0];
document.getElementByClassName('testDiv')[0].appendChild(p_node);
} else {
alert('BAD http status.');
}
} // END http.onload
} // END *****else of if (username == "") *****
} // **END submitLogin
------------
// on the server I have ../modules/logins/controllers/Logins.php:
xmlhttprequest POST request API -- how do you set up parameters from a form?
4 years ago
4 years ago
#1
4 years ago
#2
Hi mbrick02,
Your post is a little bit confusing, but I'll try my best to help you out.
Firstly, your json is invalid as it has an extra curly brace right after "authorization": "*", it should read like ths:
you can access this api endpoint via the api explorer with this url (insert your app-name):
Now I'm not sure why you have made Username, Password & Remember required fields to the endpoint or setup this way as it doesn't make sense.
I any case, you will need a method in logins called 'modal_login' to exist otherwise you would get a '404 Not Found' response
A simple method that would return 'hi' would look like this:
Provided you passed something into Username, Password & Remember
another way would be to use the get or post endpoints automatically generated by the desktop app:
With parameters set to {"id": 1} would return:
provided the first record in your database contained the above result or you could pass { "username": "mbrick02" } as a parameter and the same will be returned
I would recommend reading the docs, and having a look at some of DC's videos on api's
**** And how good do the docs look now :) - great job DC ****
https://trongate.io/docs/information/an-introduction-to-the-trongate-api-explorer
Let me know if you need a hand on the JavaScript side of things, but looking at your code above I think you aren't far off :)
Your post is a little bit confusing, but I'll try my best to help you out.
Firstly, your json is invalid as it has an extra curly brace right after "authorization": "*", it should read like ths:
you can access this api endpoint via the api explorer with this url (insert your app-name):
Now I'm not sure why you have made Username, Password & Remember required fields to the endpoint or setup this way as it doesn't make sense.
I any case, you will need a method in logins called 'modal_login' to exist otherwise you would get a '404 Not Found' response
A simple method that would return 'hi' would look like this:
Provided you passed something into Username, Password & Remember
another way would be to use the get or post endpoints automatically generated by the desktop app:
With parameters set to {"id": 1} would return:
provided the first record in your database contained the above result or you could pass { "username": "mbrick02" } as a parameter and the same will be returned
I would recommend reading the docs, and having a look at some of DC's videos on api's
**** And how good do the docs look now :) - great job DC ****
https://trongate.io/docs/information/an-introduction-to-the-trongate-api-explorer
Let me know if you need a hand on the JavaScript side of things, but looking at your code above I think you aren't far off :)
4 years ago
#3
Thanks for the quick response Dafa -- I'll play around with the code using your suggestions and see what happens.
The extra curly brace was for the whole/overall api.json file -- I just didn't have the opening curly brace.
If this helps explain the code better, what I am trying to do is a modal login form via a submit click event that runs the xmlhttprequest with username, password and remember as the parameters.
The extra curly brace was for the whole/overall api.json file -- I just didn't have the opening curly brace.
If this helps explain the code better, what I am trying to do is a modal login form via a submit click event that runs the xmlhttprequest with username, password and remember as the parameters.
4 years ago
#4
I figured it (at least this main error) out...typo on the php target function name...
Thanks again DaFa
Thanks again DaFa