Skip to content

Commit e433dbf

Browse files
committed
Add sample code for performing AJAX calls.
1 parent 578e3d4 commit e433dbf

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Insert a hidden input field named `csrf_token` in your forms.
5252
<button type="submit">Send</button>
5353
</form>
5454

55-
You can also use following javascript code to perform AJAX calls (jQuery 1.5 and newer).
55+
Javascript-code sample to perform AJAX calls with jQuery 1.5 and newer.
5656

5757
function csrfSafeMethod(method) {
5858
// HTTP methods that do not require CSRF protection.
@@ -67,6 +67,24 @@ You can also use following javascript code to perform AJAX calls (jQuery 1.5 and
6767
}
6868
});
6969

70+
$("#AJAXForm").submit(function(event){
71+
event.preventDefault();
72+
$.ajax({
73+
type: "POST",
74+
url: "/Hello",
75+
data: {
76+
name: $("#AJAXFormName").val()
77+
},
78+
success: function(data) {
79+
// Switch to HTML code returned by server on success.
80+
jQuery("body").html(data);
81+
},
82+
error: function(jqXHR, status, errorThrown) {
83+
alert(jqXHR.statusText);
84+
},
85+
});
86+
});
87+
7088
A demo application is provided in the samples directory. To launch it:
7189

7290
revel run github.com/cbonello/revel-csrf/samples/demo

0 commit comments

Comments
 (0)