I'm following a React-Redux tutorial. My axios.post() is failing when running the app, but if I use that same URL and paste it into the browser's address textbox it works. Why would that happen?
Here is the call in my app:
const request = axios.post(`${ROOT_URL}/posts/${id}${API_KEY}`);
Here is the error, as shown in F12 in Chrome:
POST http://reduxblog.herokuapp.com/api/posts/120342?key=bob884 404
(Not Found)
Why would that fail when it's a good URL? If you click it, you'll see the response in the browser:
{"id":120342,"title":"SOLID","categories":"OOP","content":"SOLID is an
acronym..."}
When you click the link, the browser sends a GET request to the server.
A POST endpoint might not be available from the server side at that specific URL address.
Whenever the server is unable to find a URL with a specified method (GET / POST), it returns a 404 - Not found error. In this case, it doesn't find any POST method defined for that address.
You should consider changing the method to a GET request, if that's what you desire.
Related
I would like to redirect from POST method to frontend page with HTTP status code 303.
Expected result is that browser after making POST request redirects to page specified in Location header.
Currently I am getting CORS failed error message and browser does not redirect to frontend page.
A redirect does not mean "Load this URL in the browser window". It means "You can get whatever you asked for here".
When you make an Ajax request using JavaScript, the response is provided to JavaScript.
If the response is a redirect, then the browser follows it automatically and provides the response to the redirect to JavaScript.
The URL you redirect to needs permission from CORS in order for the JavaScript to read the response.
Do not attempt to mix web services and regular page navigation
If you want to submit some data and load a new page: Use a form submission.
If you want to submit some data and handle the response with JS: Use Ajax.
I'm working on an ExpressJS app, after I a successful post to the end point /api/threads/{board} I want to redirect to the page /b/{board}, however I don't know which HTTP status code I need to return in res.redirect(). I looked at 3xx redirection status codes on https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3 but neither one seems to fit into my case. Should I stick with 201 for a successful post operation?
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code. If not specified, status defaults to “302 “Found”. Link http codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Redirects can be a fully-qualified URL for redirecting to a different site:
res.redirect('http://google.com')
Redirects can be relative to the root of the host name. For example, if the application is on http://example.com/admin/post/new, the following would redirect to the URL http://example.com/admin:
res.redirect('/admin')
Redirects can be relative to the current URL. For example, from http://example.com/blog/admin/ (notice the trailing slash), the following would redirect to the URL http://example.com/blog/admin/post/new.
res.redirect('post/new')
You can refer this documentation for better clarity : http://expressjs.com/en/5x/api.html#res.redirect
Yes I believe it's 201. From MDN:
201 Created The request has succeeded and a new resource has been
created as a result. This is typically the response sent after POST
requests, or some PUT requests.
Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#Successful_responses
Although, 302 is the standard status code for redirection, note that you can only use one status code at a time since headers cannot be set once they are sent.
In my application I need to open a pdf in a new window. I'm making a window.open call for that. Mine is an MVC application. The url for the window.open contains my controller name and action method so that it will hit the respective action method.
But due to some reason, when I run with http request, I'm able to open the pdf, which means my controller call returns 200. But in the case of https, my controller throws a 302. Is there any solution for this?
One more thing which I found out is, when I compose the https request in Fiddler by adding some request headers to it, my controller returns 200. But I'm not able to add the request headers in window.open. Is there any way to do this?
I cannot go for a server side coding here. I have to complete it in the js itself.
So I re-installed my database, the problem is that now, my app doesn't seem to work.
Every time I try to GET a resource in my Angular.js site, the request stalls until I get back this error:
net::ERR_EMPTY_RESPONSE
The preflight "OPTIONS" requests works fine, but the actual GET request just hangs up.
If I try the same GET request with curl, then I instantly get a correct response. The same for the browser, if I type the request in the address bar, I get a correct response.
What do you guys think may be happening here?
I have the script below:
disallow_part = {
disallow_part: '#disallow-part'
init: ->
#setListeners()
setListeners: ->
$(#disallow_part).live 'click', ->
partId = $(this).data("part-id")
augmentId = $(this).data("augment-id")
$.get("/parts/#{partId}/disallow_part_for_augment/#{augmentId}", null, 'script')
}
It seems to be hitting that route because records are being added to the db but in the console I get this error.
GET http:***/parts 404 (Not Found)
Any idea what's going on?
That's a 404 error being returned from your server: you sent the request to the wrong URL.
Examine the request you've sent on your browser's deverloper console's network tab (it'll probably show up in red, because it was unsuccessful)
Since the request seems to be understood correctly by your backend, the one plausible cause is a discrepancy in content-type headers.
Check your browser console's network section.
What does the http header look like in the request?
What does the http header look like in the response?
Have your eyes on the content-type fields. Maybe your ajax request expects json, but the server returns text/html?
Try playing with the headers of your ajax to send and expect the right data types, and I'm sure you'll fix it in no time.