Reputation: 57469
I need jQuery to post to a relative url (currently its posting to the root).
Structure:
localhost/myApp
Account/Login
localhost/myApp/Account/Login
<- need it herelocalhost/Account/Login
Form:
<form id="login" action="Account/Login" method="POST">
</form>
jQuery:
$.post($el.find('form').attr("action"), $el.find('form').serialize(), function(resp) {
});
I need to be able to put this app in any URL and it works, so if its hosted at www.site.com/dir1/dir2/
, then it should post to www.site.com/dir1/dir2/Account/Login
.
Note, this is a single page app. So the form is located at localhost/myApp
.
Headers Info:
Request URL:http://localhost/Account/Login
Request Method:POST
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:19
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost
Origin:http://localhost
Referer:http://localhost/myApp
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
X-Requested-With:XMLHttpRequest
Upvotes: 3
Views: 3249
Reputation: 34038
Request URL:http://localhost/Account/Login
Request Method:POST
Status Code:404 Not Found
Request Headersview source
...
...
Referer:http://localhost/myApp
Although you're loading /myApp, it's clear that some form of redirection is taking place that is dropping the client at the root / path:
Thus, in your action, add the following to the action attribute:
<form action="/myApp/Account/Login">
As an aside, you either have a 301/302 redirect that is rewriting the URL to http://localhost
, or you're not really visiting http://localhost/myApp
, or you could possibly have a <base>
HTML element that is causing your relative URLs to get thrown off. Without seeing all of the HTML, it's difficult to say for sure, but changing the path in the action will connect your form to the server.
If the root of your application changes from platform to platform, consider using the <base>
HTML element to avoid needing to change all of your URLS. With <base>
, you simply make the change in 1 place in the HTML.
<!-- relative urls start from /myApp -->
<base target="_blank" href="http://localhost/myApp/">
For more information, please see the Mozilla Developer Center article on the base HTML element:
href
- The base URL to be used throughout the document for relative URL addresses.
- If this attribute is specified, this element must come before any other elements with attributes whose values are URLs.
- Absolute and relative URIs are allowed (but see note section below).
Also, while information from W3Schools should be digested with some scrutiny, I list the page on the base HTML element here, simply because they have a better example than MDC:
<head>
<base href="http://www.w3schools.com/images/" target="_blank" />
</head>
<body>
<img src="stickman.gif" />
<a href="http://www.w3schools.com">W3Schools</a>
</body>
Here is the obligatory W3Fools link.
NOTE: The base element must come before any other element that refers to the URLs that are modified by the base element.
Upvotes: 1
Reputation: 833
try adding a slash in front of the url action to make it:
/Account/Login
Upvotes: 1