2016-08-16 4 views
0

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과 같은 팩토리를 구현하려했지만 성공하지 못했습니다.

어떤 문제가 있습니까? 나는 정말로 잃어버린 ...

감사합니다. ,

  1. http://localhost:3000/load?callback=JSON_CALLBACK
  2. 처음으로 https://graph.facebook.com/cocacola?callback=JSON_CALLBACK

: 좋아

편집 는, 그것이 작동하지 않는 내 코드없는 것 같다 ... 나는 두 adresses 시도 아무 것도 나타나지 않지만 두 번째로 나는 결과를 기대합니다 (참조 : exemple here). Windows 7에서 실행되는 데비안 가상 머신이 있습니다. 프록시를 사용하고 있습니다 ... 어떤 제안이 있습니까?

+1

노드 Server.js 파일에서 res.end() 대신 res.jsonp (JSON.stringify (rows))를 반환하십시오. – Sarathy

+0

아니요, res.jsonp와 동일한 결과가 있습니다. – kawai

답변

1

좋아, 해결책을 찾았습니다. 고마워요 Sarathy, 당신이 나를 길에 태워 버렸어요.

나는 Google 크롬을위한 정말 좋은 도구 인 Postman으로 URL 반환을 검사합니다.그래서, 조금 다른 있었다는 것을 알아 낸 다음 URL https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero를 들어

나는이 결과가 있습니다

[{"idType":1,"nameType":"DISIT"},{"idType":2,"nameType":"ECO"},{"idType":3,"nameType":"EXT"}] 

첫 번째에게 내 URL을 http://localhost:3000/load?callback=JSON_CALLBACK

{ 
    "name": "Super Hero", 
    "salutation": "Helo", 
    "greeting": "Helo Super Hero!" 
} 

을,이이 우편 번호로 json으로, 두 번째로 html로 재구성됩니다. 이 다른 사람 도움이 될 수 있습니다)

희망,

app.get('/load',function(req,res){ 
    connection.query("SELECT * FROM Type",function(err,rows){ 
     if(err) 
     { 
      console.log("Problem with MySQL"+err); 
     } 
     else 
     { 
      console.log("OK"); 
      res.jsonp(rows); 
     } 
    }); 
}); 

이제 모든 것이 잘 작동 : 그래서 이런 Server.js 내 복귀를 수정했습니다.