普通网友 2015-06-10 04:43
浏览 22

Ajax URL参数问题

When i do a Ajax request , for the url mentioned below , it is concatenating the url value which i have mentioned along with the pathname of the URL of the page.

JSP:

<form:form modelAttribute="createtask" id="create-task-form" name="create-task-form">
    <div class="task">
        <form:input path="taskName" placeholder="enter task"/>
        <c:url value="/createtask/${todo.todoId }" var="createtaskUrl" />
        <a id="create-task" href="${createtaskUrl }"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></a>
    </div>
</form:form>

Jquery Ajax call:

$("a#create-task").click(function(event){
    event.preventDefault();
    var todoid = this.href.substring(this.href.lastIndexOf("/")+1,this.href.length);
    var createTaskForm = $("#create-task-form").serialize();
    $.ajax({
        type        :   "POST",
        url         :   "createtask/"+todoid,
        data        :   createTaskForm,
        dataType    :   "text",
        success     :   function(data){
            console.log("Created Task successfully..");
            $("section#taskviewer").html(data);
        }
    });
});

I know it concatenates with the context path of the application. For example if my application context path is localhost:8080/SpringMVCPractice/ and the page in which I do the Ajax request is

http://localhost:8080/SpringMVCPractice/viewTodo/110

and when I do a ajax request by clicking the anchor tag, the url it takes is

http://localhost:8080/SpringMVCPractice/viewTodo/createtask/110

Why it is not taking localhost:8080/SpringMVCPractice/createtask/110 ?

Any suggestion? I am relatively new to Ajax.

  • 写回答

1条回答 默认 最新

  • weixin_33717117 2015-06-10 04:52
    关注

    You can use:

    ${pageContext.request.contextPath}
    

    by specifying the url as,

    url : "${pageContext.request.contextPath}/createtask/"+todoid,
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀