Récupération information par ajax
Bonjour,
Depuis ma dernière discussion j'ai progressé ...
mais je reste bloqué depuis 2 jours sur une anerie de ma part mais je ne comprend pas ou...
Voici mon code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Jquery._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/themes/redmond/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$(".addNew").dialog({
autoOpen: false,
width: 300,
height: 300,
modal: true,
close: function (event, ui) {
location.reload(false);
},
buttons:
{
"Add": function () {
var name = $("#<%= txtName.ClientID %>").val();
var surname = $("#<%= txtSurname.ClientID %>").val();
var age = $("#<%= txtAge.ClientID %>").val();
$.ajax({
type: "POST",
url: "Default.aspx/AddNewItem",
data: '{"name":"' + name + '", "surname":"' + surname + '", "age":' + age + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if ( msg == true) {
alert("Successfully added new item");
}
else {
alert("t'es une buse");
}
},
error: function () {
alert("Error! Try again...");
}
});
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#add").click(function (event) {
event.preventDefault();
$(".addNew").dialog("open");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" id="add">Add New</a>
<div class="addNew" title="Add new Person">
<table>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Surname
</td>
<td>
<asp:TextBox ID="txtSurname" runat="server" />
</td>
</tr>
<tr>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtAge" runat="server" />
</td>
</tr>
</table>
</div>
<asp:Repeater ID="rptShowItems" runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>
Name
</th>
<th>
Surname
</th>
<th>
Age
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Surname") %>
</td>
<td>
<%# Eval("Age") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Imports System.Web.Services
Public Class _Default
Inherits System.Web.UI.Page
<WebMethod()> _
Public Shared Function AddNewItem(ByVal name As String, ByVal surname As String, ByVal age As Integer) As Boolean
Try
Dim person = New Person()
person.Name = name
person.Surname = surname
person.Age = age
'add your logic to insert item into database
'we will always return true here, in real life, this of course can't be always true ;)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
Public Class Person
Public Property Name As String
Public Property Surname As String
Public Property Age As Integer
End Class |
Donc quand je clique sur le lien ADD, ma popup s'affiche, je rentre bien les param et quand je valide ça m'envoi les données dans ma webmethod.
Si je ne mets pas les bons params, j'ai bien ajax:error qui se déclenche.
Si je met les bons paramètres, ma méthod ajax:success fonctionne ... mais à moitié.
Code:
1 2 3 4 5 6
| if ( msg == true) {
alert("Successfully added new item");
}
else {
alert("t'es une buse");
} |
SAUF QUE C'EST TOUJOURS
Citation:
t'es qu'une buse qui ressort
alors que dans mon code behind, je renvoie true...
Que dois tester pour retrouver mon "true" ?
Merci d'avances