最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sammy.js 404 on root path - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question asked Feb 4, 2012 at 21:31 RyanRyan 4,4143 gold badges45 silver badges82 bronze badges 3
  • 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
Add a ment  | 

2 Answers 2

Reset to default 8

To 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

发布评论

评论列表(0)

  1. 暂无评论