I'm coming from the "Java world" and creating a small web program for the first time with: NextJS, React, Postgres and axios.
When I create the Login/Signin pages, I succesed to communicate with the server and send with post requests.
My problem is when I tried to send the post messages outside the render() of react, Its send the route without the url host, and the server doesnt get the post request. (When i send the post request with Postman its working).
I know it's a server/client sending requests problem , but I've been sitting on it for a few days and can not solve the problem.
So here is a limited repo of my web program: https://github.com/idonava/react-next-program-example
SignIn - working post request flow:
pages/signin.js handleSubmit -> signIn (line 103).
libs/auth.js signIn-> authenticate (line 16).
services/authApi.js authenticate-> post(line 6).
services/requets.js post send request via axios(line 8).
After that - the server get the post request, return answer to the client and the user login succesfully
Getting the user id- NOT working post request flow:
pages/flickr.js getInitialProps -> getUser (line 15).
libs/user.js getUser-> getCurrentUser (line 4).
services/authApi.js getCurrentUser-> post(line 26).
services/requets.js post send request via axios(line 8).
the server doesnt get to post request, And it look like the url is "/routing/getuser" instead of "localhost:3100/routing/getuser".
notes:
The program run locally on localhost:3100.
The server listening for the request on server.js -> routing/index.js -> db/models/user.
I'd be happy to get help with that.
Thanks a lot !!
Related
Summary
I am building a website(react app) integrated with twitch helix api.
I use the Implicit grant flow to auth my website
When request this https://api.twitch.tv/helix/videos route with game_id query param, it will always return an empty array
a picture from edge browser's network and console I printed
And the other routes are works fine.
like this route
https://api.twitch.tv/helix/games/top
or same route with id query param
https://api.twitch.tv/helix/videos
BUT I use the api tester like Thunder Client for VS Code.
The https://api.twitch.tv/helix/videos works fine
Also used the C# NET6 ConsoleApp to test on my windows 11
Found that this https://api.twitch.tv/helix/videos route only works on server side, the client side could not works.
Only this https://api.twitch.tv/helix/videos route with game_id query param return an empty array while others (same api with different route or same route with other query param) works fine, it seems this is not a CORS error
Tried Solution
Had tried this CORS - Wrong ‘Access-Control-Allow-Origin’ header on twitch developer forums to add a request header 'accept': 'application/vnd.twitchtv.v5+json', or renew my twitch developer console's clientId.
But still didn't work
My website on vercel
Does anyone know this issue or I am missing something?
Try set language request header to a empty string:
headers.set('accept-language', '')
It worked for me.
Our React app sends a GET request to our API requesting a Twilio token. Out API then requests the token from Twilio. The token is then sent in the response back to the React app. The React app then executes Twilio.Device.setup(token, {debug: true}), and this appears to happen successfully.
The Twilio.Device.instance object appears to have the token retrieved from the server, and the token matches what the server responded with AND what the server prints to the logs.
We then call Twilio.Device.connect() which results in an error message to the console :
{"payload":{"callsid":"<SOME_ID>","error":{"code":31100,"message":"Bad JSON in msg"}},"type":"hangup","version":""}
Opening the Dev console I see this is all happening in a web socket connection, and the payload sent RIGHT before this message appears (and it is reliably sent immediately before the response, so I believe it is what triggers the error) is a payload of this form:
{
"type":"invite",
"version":"1.4",
"payload":{
"sdp":"v=0\r\no=- 436124720934282410 2 IN ... A BUNCH OF DATA WITH CARRIAGE RETURNS ... f-d0582b8dc5e6\r\n",
"callsid":"TJSceeec256-b343-4d13-bf26-febd73fcd484",
"twilio":{}
}}
So the payload.sdp attribute is definitely NOT Json, but it doesn't really look like it's even trying to be, so it's not some kinda typo.
The entire body of the request is properly formed JSON though, renders and pretty print.
So what do I need to do to start an outgoing call with the Twilio 1.4 JavaScript SDK? About a month ago the dev working on this was making calls successfully, so it's definitely possible, but I'm not sure what changed.
I'm getting the same error. Changing
Twilio.Device.connect();
to
Twilio.Device.connect({x:null});
helped me to get outgoing calls working.
I have a Java Spring REST back-end (#RestController) and using as security option Json Web Tokens(no session). At the front-end I want to use JavaScript (jQuery to send requests to back-end), Html.
So after login I save a JWT in browser and send it back in header with every request I make to #RestController.
My question is: How to navigate between pages (that are accessible only for authenticated users) from js? How #RestController will work in this case?
The #RestController handle the requests containing a path (the path of the request is not necessary to be the same with the path(URL) from front-end)
Solution (if you have a front-end server): When you try to reach a front-end URL you make a call to the server-side; if the response status is 200 the page can be displayed(with the body of the response if you send information); if the response is not you will stay on the home page, or you can redirect the user to login page...
Also check this : http://www.studytrails.com/frameworks/spring/spring-security-method-level/
I'm making an GET request to my server with AJAX+JS. I'm using it to delete file like this:
delete.php?file_id=0123456789&user=555555
When I send GET request delete.php will delte file with ID 0123456789, but is there a way to accept only request that server makes to itself.
For example if user opens new tab and types www.mysite.com/delete.php?file_id=0123456789 server will decline that request, but if I call it with JS function server will accept the request.
How about using X-XSRF-TOKEN
in combination with Angular JS?
This will require your webapplication to generate and check this token, but your AJAX request will be authenticated.
I am working on a new project like jsfiddle.
I am facing a problem while development of it. When I try to send a ajax request with JS alert() function in text box the server is giving a 403 error. Can anyone help me with this?
Example:
Go to this URL: http://www.labs.codeteam.in/lab
Enter alert(1) in javascript textbox and Run the project, the server will give a 403 error in console.
A web server may return a 403 Forbidden HTTP status code in response to a request from a client for a web page or resource to indicate that the server can be reached and understood the request, but refuses to take any further action.
Status code 403 responses are the result of the web server being configured to deny access, for some reason, to the requested resource by the client.
The server might be configure to prevent script injection. Sending "alert()" with your post to the server might look like some type of injection attempt to the server.
try urlEncode your string before sending it back to your controller and then u can decode it back in php when u get it
It's actually encodeURI http://www.w3schools.com/jsref/jsref_encodeURI.asp
I have found a different way to do this.
While submitting the data in ajax request I will reverse the string so that the server cannot recognize the keywords and in php I'll reverse it back and use it.
Did you find the solution to your problem?
I happen to have the same problem, I thought my server was slowing down for some reason, but after making a ping -i .1 domain.com, from 2000 packets sent, only .1%~ was packet loss. My queries are about 2ms.
I jumped in the conclusion that it might be that the requests are too many for the server to handle at the same time (I'm doing periodical ajax request [about 1 every 4 seconds], in 2 js scripts).
The error is in your data - try sending something useless like "asdfasdfasdfas" - it will be okay, at least I pasted that in the text box, hit run, and on the url http://www.labs.codeteam.in/lab/runProject I got 200 status code. When I wrote alert(1), then indeed 403 status code was thrown - your server is treating javascript data as an attempt to inject code.