Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:32:41+00:00 2026-06-06T02:32:41+00:00

So I have successfully gotten AJAX requests to work before but I have always

  • 0

So I have successfully gotten AJAX requests to work before but I have always had to use a form, and then at the end of the submit do return false so that it doesn’t refresh the page.

I have also just recently moved my JavaScript into a separate file this has caused my @ commands to fail. Because of this I do not no how to set my URL to my route?

HTML:

<button id="saveAsDefaultButton">Save as default</button>

Playframework Java code:

public static Result saveDefaultPhoneForUser(String handset) {
    User currentUser = User.findByName(session("name"));
    currentUser.lastControlledHandset = theHandset;
    currentUser.save();
    return ok();
}

routes:

POST    /                           controllers.Application.saveDefaultPhoneForUser(handset : String)

javascript:

$('#saveAsDefaultButton').click(function(evt) {
        $('#errors').hide();
        $.ajax({
            type : 'POST',
            url : "controllers.Application.saveDefaultPhoneForUser",
            data : $('#controlledPhone option:selected').text(),
            dataType : "text",
            success : function(data) {
                //setError('Call succedded');
                //$('#test1').attr("src", data)
            },
            error : function(data) {
                setError('Make call failed');
            }
        });
        return false;
    });

I’m sure there is a way to do this but I am just having no luck finding anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:32:44+00:00Added an answer on June 6, 2026 at 2:32 am

    For this job you should go with javascriptRoutes as it generates correct JS paths based on your routes.conf. You’ll find usage sample in Zentask sample

    Anyway, for now you can fix your AJAX call by changing the url to

    url : '@routes.Application.saveDefaultPhoneForUser()',
    

    This way requires it to place the whole JS in template, which is wrong. It can or even should be moved to separate JS file and to make it possible you need to use javascriptRoutes.

    More…

    javascriptRoutes are not described yet in official documentation, but here’s step-by-step introduction to it. Although the description looks sophisticated de facto using this way brings a lot of benefits.

    1. Create the common routes

    First you need to create common routes in conf/routes file:

    GET     /item/:id     controllers.Application.getItem(id: Long)
    POST    /item/new     controllers.Application.newItem
    PUT     /item/:id     controllers.Application.updateItem(id: Long)
    

    Of course, you need to create at least these three actions in Application controller:

    • getItem(Long id){ ... }
    • newItem() { ... }
    • updateItem(Long id) { ... }

    2. Create an action translating common routes to JS

    • place it somewhere, ie. in your Application controller
    • Let’s call it javascriptRoutes()

    In that action you’ll point the existing routes from the conf/routes file

    public static Result javascriptRoutes() {
        response().setContentType("text/javascript");
        return ok(
            Routes.javascriptRouter("myJsRoutes",
                routes.javascript.Application.getItem(),
                routes.javascript.Application.newItem(),
                routes.javascript.Application.updateItem(),
                //inside somepackage
                controllers.somepackage.routes.javascript.Application.updateItem()
            )
        );
    }
    

    Note: Don’t set any params in brackets.

    3. Create a route for javascriptRoutes action and include it in your template

    Route conf/routes

    GET     /javascriptRoutes     controllers.Application.javascriptRoutes
    

    View in <head> part of /views/main.scala.html

    <script type="text/javascript" src='@routes.Application.javascriptRoutes()'></script>
    

    4. Use javascriptRoutes where you want

    Up from now you can use routes in JS to get the correct path without need to specify the url and type. For an example instead of:

     $('.getAjaxForThisContainer').click(function(e) {
        var idToGet = $("#someField").val();
        $.ajax({
            type : 'GET',
            url : '@routes.Application.getItem()',
            data : {
                id: idToGet
            },
            success : function(data) {
                // ... some code on success
            }
        });
        return false;
    });
    

    you can use simplified version (myJsRoutes from point 2):

    myJsRoutes.controllers.Application.getItem(idToGet).ajax({
        success : function(data) { ... some code ... }
    });
    

    or

    myJsRoutes.controllers.Application.newItem().ajax({
        success : function(data) { ... some code ... }
    });
    

    etc…

    • you don’t need to specify type: "POST" – JS router will use correct method according to conf/routes rule
    • you can set id of the record (or other params) to GET or PUT (or other methods) using routes-like syntax in pure JS
    • If your route rule contains all required params you can really minimize yours JS:

    for route:

    GET   /some/:a/:b/:c    controllers.Application.getABC(a: String, b: Integer, c: String)
    

    JS:

    myJsRoutes.controllers.Application.getABC("a", 1, "b" ).ajax({});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully gotten my low-level mouse hook code to work, but there are
Have successfully gotten the csrf middleware working in express as per previous SO questions.
I have successfully gotten Dreamweaver to accept my DLL and call it's methods. The
I have a PHP mail script that successfully sends emails to everything BUT GMail
Has anyone successfully gotten swfaddress to work with IE8 and above? It seems that
I realise this question has been asked before however the previous answers have gotten
I have a multistop gradient created with colorzilla that I have successfully gotten to
In response to my question about Windows API's, I have successfully gotten it to
I have a QuickLook plugin which I have successfully debugged and gotten working under
I have successfully gotten a bunch of Twitter user_ids using the Twitter API resource

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.