Last active
June 10, 2024 15:43
-
-
Save ChrisMoney/dbd69cd6975b2fc6f0b6db8a87d473fe to your computer and use it in GitHub Desktop.
JS - Javascript HTTP Request Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xhr = new XMLHttpRequest; | |
xhr.open("GET", url); | |
xhr.send(); | |
// ******************************** | |
// Return Json object from server | |
// ******************************** | |
var id = 1; | |
var req = new XMLHttpRequest(); | |
req.overrideMimeType("application/json"); | |
req.open('GET', url?ServerSideHandler.aspx?Event='Test&Id='+id, true); | |
req.onload = function() { | |
var jsonResponse = JSON.parse(req.responseText); | |
// do something with jsonResponse | |
}; | |
req.send(null); | |
// ****************************************************** | |
// Retrieve XMLHttpRequest response from JS back to the server | |
// ****************************************************** | |
// ServerSideHandler.aspx | |
Event = "Test' | |
int id = Request.QueryString("id); | |
var obj = GetData(id); | |
var json = new JavaScriptSerializer().Serialize(obj); | |
Response.Clear(); | |
Response.ContentType = "application/json; charset=utf-8"; | |
Response.Write(json); | |
Response.End(); // data now goes back to the calling JS XMLHttpRequest object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment