JQuery || javascript || html || apache || node.js (med express)

Trädvy Permalänk
Medlem
Registrerad
Sep 2009

JQuery || javascript || html || apache || node.js (med express)

Hej, vill börja med att säga att jag inte är någon hejare web programmering därav risk för dumma fel.

Bilder från REST client (plugin för att rest api):

Node.js koden:

module.exports = require('./lib/express'); var express = require('express'); var app = express(); /*This is the variable that inkludes all of the courses.*/ var courseArray = [{"_id":1,"courseId":"DT162G","courseName":"Javascript-baserad webbutveckling","coursePeriod":1}, {"_id":2,"courseId":"IK060G","courseName":"Projektledning","coursePeriod":1}, {"_id":3,"courseId":"DT071G","courseName":"Programmering i C#.NET","coursePeriod":2}, {"_id":4,"courseId":"DT148G","courseName":"Webbutveckling för mobila enheter","coursePeriod":2}, {"_id":5,"courseId":"DT102G","courseName":"ASP.NET med C#","coursePeriod":3}, {"_id":6,"courseId":"IG021G","courseName":"Affärsplaner och kommersialisering","coursePeriod":3}, {"_id":7,"courseId":"DT069G","courseName":"Multimedia för webben","coursePeriod":4}, {"_id":8,"courseId":"DT080G","courseName":"Självständigt arbete","coursePeriod":4}]; /* Gets the index from course array. Value is the value of _id we are looking for. */ function getIndexOfObject (value){ var tmpObj = Object; for(var i = 0, len = courseArray.length; i < len; i++){ if(courseArray[i]._id == value){ tmpObj = courseArray[i]; } } return courseArray.indexOf(tmpObj); } /* Sends out all the courses from courseArray. */ app.get('/courses', function (req, res) { res.json(courseArray); }); /* Get function for rest api. It gets a specific index from our courseArray. */ app.get('/courses/:id', function (req, res){ // Get the number of the id we are looking for. var idToShow = req.params.id; // check so that the index exists. var tmpIdx = getIndexOfObject(idToShow); // If tmpIdx is with in the array bondrays. if(tmpIdx != -1){ // Resturn the object. res.json(courseArray[tmpIdx]); } else{ // Only here to return something. // I don't wanna return a res.sendStatus(200); here. res.json(courseArray[courseArray.length]); } }); app.delete('/courses/:id', function (req, res){ // Get the id we want to delete. var idToDelete = req.params.id; // Get index to delte. var tmpIdx = getIndexOfObject(idToDelete); // Check so that the in dex is in the Array. if(tmpIdx != -1){ // Delete the index with the right id. courseArray.splice(tmpIdx, 1); } // returns to client that the operation is OK res.sendStatus(200); }); // Set up the lisning port for the server. var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });

html koden för apache2 servern:

<!DOCTYPE html> <html> <head> <title>using api in nodejs</title> <meta charset="UTF-8"> <!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min..."></script>--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.mi..."></script> <script type="text/javascript" src="FrontJS.js"></script> </head> <body> <h1 id="h01"></h1> <p></p> </body> </html>

Javascript/jQuery koden i apache:

// To keep all data in. var all; /*jQuery.ajax ({ url: "http://127.0.0.1:3000/courses", type: "GET", // data: JSON.lstringify({data:"test"}), dataType: "json", contentType: "application/json; charset=utf-8", success: function(result){ $.each(result, function (i, field) { $("p").append(field + " "); }); } });*/ function getdata(){ // $.getJSON("127.0.0.1:3000/courses", function (result){ $.get("127.0.0.1/courses/1", function(result){ $.each(result, function (i, field) { $("p").append(field + " test "); }); }); } function gethead(){ $("#h01").html("check so jQuery is working."); } $(document).ready(function(){ gethead(); getdata(); //alert(all); });

Det som händer är att jag får popup med texten "undefined" alert ruta (ser det ut som iaf). Det jag önskar att få veta är:

  • Är det något annat jag ska använda?

  • jag ens får emot någon data?

  • Vad är den föredragna metoden för att hämta data från node.js?

Tack för svar!

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6

Trädvy Permalänk
Medlem
Registrerad
Sep 2009

update:
FrontJS.js

// To keep all data in. var all; /*jQuery.ajax ({ url: "http://127.0.0.1:3000/courses", type: "GET", // data: JSON.lstringify({data:"test"}), dataType: "json", contentType: "application/json; charset=utf-8", success: function(result){ $.each(result, function (i, field) { $("p").append(field + " "); }); } });*/ function getdata(){ // $.getJSON("127.0.0.1:3000/courses", function (result){ /*$.get("127.0.0.1/courses/1.json", function(result){ var items = []; $.each( result, function( key, val ) { alert( "<li id='" + key + "'>" + val + "</li>" ); }); });*/ /*$.getJSON('https://graph.facebook.com/zombies', function(result){ document.write(result.message); });*/ $.ajax({ url: 'http://127.0.0.1:3000/courses/1', method: 'GET' }).then(function(data) { alert(data); }); } function gethead(){ $("#h01").html("check so jQuery is working."); } $(document).ready(function(){ gethead(); getdata(); });

nodejs update:

/* * Author: mifa1100 * Asssigment: Node.js, Express. */ 'use strict'; module.exports = require('./lib/express'); var express = require('express'); var app = express(); /*This is the variable that inkludes all of the courses.*/ var courseArray = [{"_id":1,"courseId":"DT162G","courseName":"Javascript-baserad webbutveckling","coursePeriod":1}, {"_id":2,"courseId":"IK060G","courseName":"Projektledning","coursePeriod":1}, {"_id":3,"courseId":"DT071G","courseName":"Programmering i C#.NET","coursePeriod":2}, {"_id":4,"courseId":"DT148G","courseName":"Webbutveckling för mobila enheter","coursePeriod":2}, {"_id":5,"courseId":"DT102G","courseName":"ASP.NET med C#","coursePeriod":3}, {"_id":6,"courseId":"IG021G","courseName":"Affärsplaner och kommersialisering","coursePeriod":3}, {"_id":7,"courseId":"DT069G","courseName":"Multimedia för webben","coursePeriod":4}, {"_id":8,"courseId":"DT080G","courseName":"Självständigt arbete","coursePeriod":4}]; /* Gets the index from course array. Value is the value of _id we are looking for. */ function getIndexOfObject (value){ var tmpObj = Object; for(var i = 0, len = courseArray.length; i < len; i++){ if(courseArray[i]._id == value){ tmpObj = courseArray[i]; } } return courseArray.indexOf(tmpObj); } /* Sends out all the courses from courseArray. */ app.get('/courses', function (req, res) { res.json(courseArray); }); /* Get function for rest api. It gets a specific index from our courseArray. */ app.get('/courses/:id', function (req, res){ // Get the number of the id we are looking for. var idToShow = req.params.id; // check so that the index exists. var tmpIdx = getIndexOfObject(idToShow); // If tmpIdx is with in the array bondrays. if(tmpIdx != -1){ // Resturn the object. res.json(courseArray[tmpIdx]);//courseArray[tmpIdx]); } else{ // Only here to return something. // I don't wanna return a res.sendStatus(200); here. res.json(courseArray[courseArray.length]); } }); app.delete('/courses/:id', function (req, res){ // Get the id we want to delete. var idToDelete = req.params.id; // Get index to delte. var tmpIdx = getIndexOfObject(idToDelete); // Check so that the in dex is in the Array. if(tmpIdx != -1){ // Delete the index with the right id. courseArray.splice(tmpIdx, 1); } // returns to client that the operation is OK res.sendStatus(200); }); // Set up the lisning port for the server. var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6