mysql 데이터베이스와 함께 localhost에 간단한 프로젝트가 있습니다. 데이터베이스에서 웹 페이지로 데이터를 표시하려고합니다. Server.js 파일을 작성하기 위해 Express와 NodeJS를 사용합니다.AngularJS jsonp call 404 상태
var express = require("express");
var mysql = require("mysql");
var app = express();
/*
* Configure MySQL parameters.
*/
var connection = mysql.createConnection({
host : "localhost",
user : "root",
password : "root",
database : "dbAngular"
});
connection.connect(function(error){
if(error)
{
console.log("Problem with MySQL"+error);
}
else
{
console.log("Connected with Database");
}
});
/*
* Configure Express Server.
*/
app.use(express.static(__dirname + '/app'));
/*
* Define routing of the application.
*/
app.get('/',function(req,res){
res.sendfile('index.html');
});
app.get('/load',function(req,res){
connection.query("SELECT * FROM Type",function(err,rows){
if(err)
{
console.log("Problem with MySQL"+err);
}
else
{
res.end(JSON.stringify(rows));
}
});
});
/*
* Start the Express Web Server.
*/
app.listen(3000,function(){
console.log("It's Started on PORT 3000");
});
내 첫 번째 문제는 내 응용 프로그램 http://localhost:8000/로 설정하고 내 데이터베이스 요청이 http://localhost:3000/로 설정되어 있었다. 교차 도메인 오류.
angular.controller('DictionaryController',['$scope','$http',DictionaryController]);
function DictionaryController($scope,$http,$templateCache, FacebookFeed) {
$http.jsonp('http://localhost:3000/load?callback=JSON_CALLBACK')
.success(function (data, status, headers, config) {
alert('success');
})
.error(function (data, status, headers, config) {
alert('error'); // Always goes here
});
}
나는 오류가없는 angular.module ('사전', [ 'ngRoute', 'ngResource를'])하지만, JSONP가 :
그래서 나는이 같은 JSONP 호출을 수행 할 호출은 항상 데이터가없고 오류 상태로 전달되고 상태는입니다.
General
Request URL:http://localhost:3000/load?callback=angular.callbacks._0
Request Method:GET
Status Code:200 OK
Remote Address:localhost:3000
Response Headers
Connection:keep-alive
Date:Tue, 16 Aug 2016 09:37:58 GMT
Transfer-Encoding:chunked
X-Powered-By:Express
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:3000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Query String Parameters
callback:angular.callbacks._0
및 응답 탭 : 나는 크롬, 헤더 탭에서 볼 수 있다면
[{"idType":1,"nameType":"DISIT"},{"idType":2,"nameType":"ECO"},{"idType":3,"nameType":"EXT"}]
그 잘못이 무엇인지 이해하지 않습니다. 나는 많은 가설을보고 시험해 보았지만 그들 중 누구도 나를 위해 일하지 않는다. JSONP와 Express/Node API 간의 지원 문제입니까? (참조 : JSONP request gives 404 in Angular app)
아니면 Resquest을 구현해야합니까?
Server.js에서 CORS를 허용하고 exemple과 같은 팩토리를 구현하려했지만 성공하지 못했습니다.
어떤 문제가 있습니까? 나는 정말로 잃어버린 ...
감사합니다. ,
- http://localhost:3000/load?callback=JSON_CALLBACK
- 처음으로 https://graph.facebook.com/cocacola?callback=JSON_CALLBACK
: 좋아
편집 는, 그것이 작동하지 않는 내 코드없는 것 같다 ... 나는 두 adresses 시도 아무 것도 나타나지 않지만 두 번째로 나는 결과를 기대합니다 (참조 : exemple here). Windows 7에서 실행되는 데비안 가상 머신이 있습니다. 프록시를 사용하고 있습니다 ... 어떤 제안이 있습니까?
노드 Server.js 파일에서 res.end() 대신 res.jsonp (JSON.stringify (rows))를 반환하십시오. – Sarathy
아니요, res.jsonp와 동일한 결과가 있습니다. – kawai