2012-06-08 4 views
0

누군가가 다음을보고 내 서버가 내 아약스로부터 어떤 전화를받지 못하고 있는지 말해 줄 수 있습니까? 경고 팝업하지만 아무것도 서버에 ??Ajax가 서버에 게시되지 않는 이유는 무엇입니까?

JSP 코드 :

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>This is a project to show how to use RESTful</title> 
</head> 
<body> 

<script src="<%=request.getContextPath()%>/js/jquery.js"></script> 
<script src="<%=request.getContextPath()%>/js/add.js"></script> 


<H1>Add Employee</H1> 

<p> 
<form name="htmlform"> 
<table border=1> 
    <thead><tr> 
     <th>ID</th> 
     <th>Name</th> 
     <th>Email</th> 
    </tr></thead> 

    <tr> 
     <td><input type="text" name="ID" maxlength="5" size="3"></td> 
     <td><input type="text" name="Name" maxlength="10" size="10"></td> 
     <td><input type="text" name="Email" maxlength="10" size="10"></td> 
    </tr> 

</table> 
<input type="button" value="Save Employee" onclick="doAjaxPost();" /> 
<p> 
<p> 
</form> 
[<a href="http://localhost:8080/RESTful/service/employees">List all Employees</a> | <a href="add.jsp">Employee Form Test</a>] 


</body> 
</html> 

ADD.JS 코드 :

function doAjaxPost() { 

    alert("doAjaxPost called"); 

     $.ajax({ 
      contentType : "application/json", 
      dataType : 'json', 
      type : "PUT", 
      url : contexPath + "/service/employee", 
      data : $(this).serializeObject(), 

      success : function(data) { 
       alert("Thanks for submitting. \n\n" + response.result); 
       // response 
      }, 
      error : function(request, status, error) { 
        alert('Error: ' + e); 
      } 
     }); 
    } 
+0

자바 스크립트 오류가없고 네트워크 오류가 없는지 확인하려면 브라우저의 콘솔을 살펴보십시오. –

답변

0

가능성은 당신이 잘못 철자 라인에 contexPath은 (contextPath를 가정하는 것은 어딘가에 정의) :

url : contexPath + "/service/employee", 

편집 : 당신이 얻고있는 상황을 보면이 작동합니다 :

url : "<%=request.getContextPath()%>/service/employee", 
+0

Downvote 순찰, 쉬운 동생. 여기에 몇 가지 의견을 내게 도와주세요. –

2
url : contexPath + "/service/employee", 

contexPath를 정의하지 않을 가능성이 있습니까?

+1

괜찮 았어. 나는 그걸 대신 주석에 넣을 수 있었지만 좀 덜 공격적 일 수 있었다. ... – Geekfish

+0

+1, 당신이 목표에 맞았다는 것을 알 수있다. @ 디스트로이, 쉬운 남자. –

0

두 가지 대답이 모두 유효합니다. 그러나 jsp 페이지에서 스크립틀릿을 사용하는 것은 나쁜 일입니다.

대신에 "/ service/employee"을 인수로 url:에 전달하면됩니다. 적절한 매핑 또는 URL이 있으면이 값을이 페이지의 현재 URL에 추가하기 만하면 실행됩니다.

저는 이것을 내 응용 프로그램에 사용했고 매우 잘 작동했습니다.

희망이 도움이됩니다. 건배.

관련 문제