I'm trying to define some routes with sammy.js here is my code:
$.sammy("#main_content", function()
{
this.get("#!/about", function()
{
// Code
});
this.get("#!/", function()
{
// Code
});
}).run();
If I goto www.mywebsite I always get a 404 error. I've tried putting a blank route in like this.get("", function() {});
and that seems to work for preventing the 404 error but then none of my normal links on the page work. How do I go about fixing this?
I'm trying to define some routes with sammy.js here is my code:
$.sammy("#main_content", function()
{
this.get("#!/about", function()
{
// Code
});
this.get("#!/", function()
{
// Code
});
}).run();
If I goto www.mywebsite. I always get a 404 error. I've tried putting a blank route in like this.get("", function() {});
and that seems to work for preventing the 404 error but then none of my normal links on the page work. How do I go about fixing this?
- If you are getting a 404 error going to the root of your site, it sounds much more like a server configuration issue to me. Do you know what the servers default document is set to? In Apache, this would be the DirectoryIndex directive in your httpd configuration file. Depending upon the server, it can be index.html or index.htm. If one of those pages does not exist, than going to the root of the site will cause this error, since the server won't know what page to pull up. If that's not the issue, then I might not understand the issue pletely. – Jeremy Vanderburg Commented Feb 5, 2012 at 0:16
- No sorry when I meant a 404 error, its a javascript error returned from sammy.js. but I only get that if I don't have a route setup for a blank route i.e. this.get("", function() {}); – Ryan Commented Feb 5, 2012 at 19:29
- Ah...okay. The 404 error seems like a page not found error, but perhaps it isn't in this case. It might be worth using Fiddler2 or Wireshark to get a packet capture of what is going on there. It might be that sammy.js is trying to pull something down, but I don't know. Hope you find an answer out there - sounds like I'm not the one who has it though. – Jeremy Vanderburg Commented Feb 5, 2012 at 22:10
2 Answers
Reset to default 8To handle the initial request that doesn't contain a hash, use the empty route you mentioned at the bottom of your list of routes
/* Get Home view for empty hash URLs
*/
this.get("", function () {
this.app.runRoute("get", "#Home");
});
In order for normal links on your site to work, i.e. links to binary files that AJAX requests don't handle, set up your anchor elements with a hash route and parameters as follows (code uses Razor syntax)
<a href="@Url.Content("#Reporting/Excel/" +
Model.Report.Type.ToString() + "/" +
Model.Report.Audience.ToString() + "/" +
Model.Report.UnitID.ToString() + "/" +
Model.Report.DepartmentID.ToString())">Download Excel version</a>
Now create a route for Sammy to send to the actual URL
/* Reporting Excel requests
*/
this.get("#Reporting/Excel/:type/:audience/:unitID/:departmentID", function () {
location.assign("Report/Excel?" +
"Type=" + this.params.type + "&" +
"Audience=" + this.params.audience + "&" +
"UnitID=" + this.params.unitID + "&" +
"DepartmentID=" + this.params.departmentID
);
});
Just override not found:
this.notFound = function(){ // do something }
You can see it here:https://github./quirkey/sammy/issues/67