
const express = require('express');
const app = express();
const port = 3000;
var path = require('path');
var repo = {
"I042416": "Jerry",
"I042417": "Tom",
"I042418": "Jim"
}
app.use(express.static(path.join(__dirname, 'public')));
app.get('/request', (request, response) => {
console.log("The employee ID sent from Client:" + request.query.id);
response.json({
UserName: repo[request.query.id] + " ( handled in port 3000 )"
});
});
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
<html>
<body>
<form action="">
ID: <input type="text" id = "inumber" name="ID" value="I042416"><br>
<input type="submit" value="Submit">
</form>
</body>
<script src="jquery1.7.1.js"></script>
<script>
$(document).ready(function(){
$("form").click(function(e){
e.preventDefault();
var data = {
id: $("#inumber").val()
};
$.ajax({
type: 'GET',
data: data,
url: 'http://localhost:3000/request',
dataType: 'json',
success: function(data) {
alert(data.UserName);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('error ' + textStatus + ' ' + errorThrown);
}
});
});
});
</script>
</html>
app.get('/request_jsonp', (request, response) => {
console.log("This service supports JSONP now: " + request.query.id);
var data = "{" + "UserName:'" + repo[request.query.id] + " ( handled in port 3001 )'"
+ "}";
var callback = request.query.callback;
var jsonp = callback + '(' + data + ');';
response.send(jsonp);
response.end();
});
$.ajax({
type: 'GET',
data: data,
url: 'http://localhost:3001/request_jsonp',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
success: function(data) {
alert(data.UserName);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('error ' + textStatus + ' ' + errorThrown);
}
});
METHOD if_http_extension~handle_request.
DATA(lv_userid) = server->request->get_form_field( 'id' ).
DATA(lv_callback) = server->request->get_form_field( 'callback' ).
DATA(lv_name) = COND #( WHEN lv_userid = 'I042416' THEN 'Jerry' ELSE 'Unknown').
DATA(lv_response) = |\{ UserName:'{ lv_name }'\}|.
DATA(lv_jsonp) = |{ lv_callback }({ lv_response });|.
server->response->append_cdata(
data = lv_jsonp
length = strlen( lv_jsonp ) ).
ENDMETHOD.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
23 | |
22 | |
15 | |
13 | |
10 | |
9 | |
7 | |
7 | |
6 |