2014-09-04 2 views
3
DISCLAIMER: This question is a question about question. So that makes this question a meta question. It does not have any connection to the previously asked questions. If you find any resemblance, lemme tell you one thing- it's purely coincidental. 

웹 페이지에서 AJAX 요청을 만들고 싶습니다. 나는 이것을 해보려고 노력해 왔지만 그 방법들 중 어느 것도 완벽하게 작동하지 못했습니다. 현실과 가까운 것을 발견 한 유일한 게시물은 this입니다.요청한 리소스에 Access-Control-Allow-Origin 헤더가 없습니다.

다른 유사한 방법으로 SO &에서 여러 다른 방법을 시도했지만 그 모든 게시물은 나에게 단 한 가지만 말했습니다.

"No 'Access-Control-Allow-Origin' header is present on the requested resource." 

이제이 질문과 유사한 질문이 있으므로이 질문에 중복으로 표시 할 것입니다. 지금 .Lemme는 '한 가지를 말해 줘. 나는 so에서 발견 된 sh * t의 모든 조각을 시험해 보았지만 아무도 그에게 내가 찾던 결과를주지 않았다. 그들 모두가 잘못 되었기 때문이 아닙니다. 왜냐하면 나는 그들을 사용하는 법을 알지 못하기 때문입니다. 그런 다음 마침내 ... 나는 위에 제공된 링크에 정착했다. 그것은 쉽습니다. 그러나 코드에 관한 어떤 것들을 알아야합니다. 이것은 아름답게 sodding 두문자 인 CORS를 처음 들었을 때입니다. 그래서 누군가 내가 내가 제기 한 질문을 이해하도록 도울 수 있다면, 모든 것에 대해 찬성표를 던집니다. 올해 3 번째 생일을 축하하기 전에이 아들 -가 - b * tch 질문을 해결하고 싶습니다. 나는 자원의 형태로 내가 가지고있는 것 모두를 & 질문으로 말할 것이다.

1) A rotten server located at Elizabeth town. 
2) I need to access it. 
3) I am planning to make a HTTP GET request. 
4) I have a url. (eg. http://whereismyweed.com) 
5) I store it into a JavaScript variable. var stoner='http://whereismyweed.com' 
6) I have a HTML div tag in my webpage. (<div id="myprecious"></div>) 
7) I wanna display the response I get from the server inside of 'myprecious' div. 
8) And last but not the least... my AJAX function. (Courtesy: some website I visited) 




$.ajax({ 
       url: stoner, 
        data: myData, 
        type: 'GET', 
        crossDomain: true, // enable this 
        dataType: 'jsonp', 
        success: function() { alert("Success"); }, 
       error: function() { alert('Failed!'); }, 
       beforeSend: setHeader 
      }); 

'myData'란 무엇입니까 ?? 무엇이 포함되어 있습니다. 이 요청에 대한 응답을 얻으려면 어떻게해야합니까? 'setHeader'란 무엇입니까 ?? 그것은 어떤 의미가 있습니까 ??? 어떻게하면 myprecious div에 응답을 표시 할 수 있습니까? 이 기능에 어떤 변화를 주어야합니까? 이 기능이 맞습니까?

너무 많은 질문이 있으면 글쎄 ... 나는 그것에 대한 단 하나의 공통적 인 대답이 필요합니까?

+0

서버를 달성 할 아래 correct.Follow 당신이에 대한 액세스 권한을 부여 할 필요가있다. HTTP를 사용하는 경우, 서버는 '* (모든 웹 사이트의 모든 요청에 ​​대해) 값 또는'http : // foo'와 같은 도메인 이름을 가진'Access-Control-Allow-Origin'이라는 속성을 헤더에 보내야합니다 .domain.com'은'http : // foo.domain.com' 만 요청을 수행하도록 허용합니다. – balexandre

+3

당신이 무슨 말을하는거야 ?? 액세스 제어 허용 - 원본 : * ??? var xhr = createCORSRequest ('GET', url); xhr.setRequestHeader ( '액세스 제어 허용 원본', '*'); xhr.send(); 참조 : http://www.html5rocks.com/en/tutorials/cors/ @balexandre –

+2

응답에 ** 보낼 필요가있는 서버입니다 ** ... 서버가 아닙니다! 너는 너의 편에서 아무것도 할 수 없다. 이렇게하면 해당 서버를 소유하거나 권한을 가질 수 있습니다. – balexandre

답변

-1

함수는 단계 "엘리자베스 타운"에서 당신의 goal-

//for getting response modify your code like 

     success:function(response){ 
      alert(response); 
      $('#myprecious').html(response); //myprecious is id of div 
    } 

    // myData variable is jSon object which contains request parameter has to send.Eg. 
    var myData = {'first_name':'Foo','last_name':'Bar'} // now on server first_name and last_name treated as request parameter. 


    // If server not required any special headers to validate request 'setHeader' does not require. by default $.ajax will take care of it. you can remove it. 



    /// final code looks like 


     $.ajax({ 
      url: stoner, 
       data: myData, 
       type: 'GET', 
       crossDomain: true, // enable this 
       dataType: 'jsonp', 
       success: function(response) { $('#myprecious').html(response);  
    }, 
       error: function() { alert('Failed!'); } 

     }); 
+1

이 스크립트를 실행하면 '실패!' 내가 얻는 결과이다. 요청의 유효성을 검사하기 위해 특별한 헤더를 추가하는 방법 ??? @Vicky –

+0

먼저 필요한 특수 헤더를 결정해야합니다. 헤더 추가 - beforeSend : function (xhr) {xhr.setRequestHeader ('X-Test-Header', 'test-value'); xhr.setRequestHeader ('X-Test-Header2', 'test-value2'); 등등 } – Vicky

관련 문제