custom href giving 404 error

custom href giving 404 error

lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0
$(document).ready(function() {    
   var editor = $('#users').DataTable({
           "processing" : true,
           "responsive": true,
           "ajax" : {
               "url" : "findall",
               "dataSrc":"",
               "type" : "POST",
               "dataType" : "json",
               "contentType" : "application/json",
               "data" : function(d) {
                   return JSON.stringify(d);
               }
           },
            "columns": [
                   {"data": "id"},
                   {"data": "name"},
                   {"data": "age"},
                   {"data": "gender"},
                   {"data": "country"},
                   {"data": "email"},
                   {"data": "mobile"},
                   {"data": "contact"},
                   {"data": "birthdate"},
                   {"mRender": function ( data, type, row ) {
                       return '<a href="@{/edit/{id}(id=${user.id})}">Edit</a>';}
                   },
                   {"mRender": function ( data, type, row ) {
                       return '<a href="@{/delete/{id}(id=${user.id})}">Delete</a>';}
                   }
               ]
           });
 });

clicking on the href is giving me 404 error. anyone encounter this before?

Replies

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    The place to start is to look at your server log to find out why its returning the 404 not found error.

    Kevin

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0

    its a 500 error, as shown below the screenshot, which does not tells me much :(

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Thats a 500 Internal Server error. Both 404 and 500 errors are error responses from the server. You will need to look at the web server logs to find out why the server script is responding with those codes. There is nothing that can be debugged from the client for these errors.

    Kevin

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0

    How do i check server log?? i went to the network tab that is what it is showing me

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0
    edited December 2020

    this is the correct url i was expecting:

    http://localhost:8080/edit/1
    

    but somehow when i click the button, the return url is

    http://localhost:8080/%7B/edit/%7Bid%7D(id=$%7Buser.id%7D)%7D
    
  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0
    edited December 2020

    previously i was using it this way and it return me the correct

    <td><a th:href="@{/edit/{id}(id=${user.id})}">Edit</a></td>
    

    i use the same href, but it is not working for this..confuse @kthorngren

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    How do i check server log??

    Do you have a web server running on your locahost to receive the request for http:localhost:8080/....?

    Doesn't seem like what you have following that URL @%... is valid. You are building the URL like this:

    '<a href="@{/edit/{id}(id=${user.id})}">Edit</a>'
    

    I don't think the macro you have will work in the columns.render. Maybe something like this:

                       {"mRender": function ( data, type, row ) {
                           //return '<a href="@{/edit/{id}(id=${user.id})}">Edit</a>';}
                           return '<a href="/edit/{id}/(id=' + row.id + )">Edit</a>';}
                       },
    

    Might not be exactly right but hopefully it will get you started. See this example and the URL example in the columns.render docs.

    Kevin

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0
    edited December 2020

    so i change it to this, since i need row.id to be dynamic , and i use parseInt(row.id), as row.id returns a string but i need it to be long.

      {"mRender": function ( data, type, row ) {
            return data = '<a href=/edit/"'  +  parseInt(row.id) +'">Edit</a>'},
        "sortable": false
       },
    
    

    but i am getting this error. Is there a way to change my int to long? or directly from string to long??

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0

    so what i have also tried is, at my backend, i try and change it to int, but still getting the same error

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Is there a way to change my int to long?

    Since you are imbedding it into a string it doesn't really matter if its an int or long because its being converted to a string. Maybe the problem is the quotes around the number. Notice the error has For inout string: ""1"". You will need to look at the docs for the URL recipient to see how to format the URL.

    Kevin

  • lieyongchanglieyongchang Posts: 29Questions: 2Answers: 0

    this is the correct format. I am going to leave it here, for people that needs it in the future.

    return '<a href=/edit/'  + row.id +'>Edit</a>'}
    
This discussion has been closed.