我正在尝试使用 ajax 从一个页面传递 2 个输入值,如果它成功,那么它会在错误时提醒其他内容。当我在 google chrome 或 mozilla firefox 中运行时,我在控制台中没有收到任何错误。所以我认为没有错误,但警报没有出现。也没有警报错误。这是 html 部分的 jsffidle 以下行在 html 页面中的 head 标签中
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet">
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap-responsive.css" rel="stylesheet">
<script type="text/javascript" src="https://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
这是jsp代码
<%!String number,message; %>
<%
number=request.getParameter("toNumber");
message=request.getParameter("body");
out.println(number+" "+message);
%>
请为此提供解决方案。
这是完整的 JSP 代码
<!DOCTYPE html>
<html>
<head>
<title>Twilio Messages (Send message Example)</title>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet">
<link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap-responsive.css" rel="stylesheet">
<script type="text/javascript" src="https://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
<script >
$(document).ready(function(){
$('#openAlert').click(function(){
alert("hi");
var number = $('#number').val(); // If its a text input could use .text()
var msg = $('#body').val(); //If its a text input could use .text()
$.ajax(
{
type: "GET",
url: "messageSending.jsp", //Your full URL goes here
data: { toNumber: number, body: msg}
success: function(data, textStatus, jqXHR){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown){
alert("error");
}
});
});
});
</script>
</head>
<body><span id="res"></span>
<div class="container">
<div class="row">
<div class="span12">
<h2>Twilio Messages (Send message Example)</h2>
<form class="form-signin" action="#" method="post">
<div class="row">
<div class="span3">
Enter Number to send:
</div>
<div class="span3">
<input type="text" name="toNumber" id="number" maxlength="13" placeholder="Enter 10 digits number to send" value="+917676462182"/>
</div>
<div class="span6">
<div class="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
The number to send an SMS to. This field accepts formatted and unformatted US numbers, e.g. +14155551212, (415) 555-1212 or 415-555-1212.<hr />
To send message from SandBox Account. The Number has to be <a href="https://www.twilio.com/user/account/phone-numbers/verified" target="_blank">verified</a>
</div>
</div>
</div>
<div class="row">
<div class="span3">
Enter Message to send:
</div>
<div class="span3">
<textarea name="body" maxlength="160" placeholder="Enter message to send" id="body">
</textarea>
</div>
<div class="span6">
<div class="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
The text of the message you want to send, limited to 160 characters.
</div>
</div>
</div>
<div class="row">
<div class="span3">
</div>
<div class="span9">
<button class="btn" type="submit" id="openAlert" >Send</button>
</div>
</div>
</form>
</div>
</div>
<div id="le-alert" class="alert alert-warn alert-block fade">
<button href="#" type="button" class="close">×</button>
<h4>Successful</h4>
<p>Message sent successfully</p>
</div>
</div>
</body>
</html>
这是完整的 messageSending.jsp 代码
<%@ page import="com.twilio.sdk.TwilioRestException" %>
<%@ page import="sms.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!String number,message; %>
<%
number=request.getParameter("toNumber");
message=request.getParameter("body");
out.println(number+" "+message);
%>