2016-08-15 2 views
0

작동하지 않음 "catch되지 않은 구문 에러 : 예기치 않은 토큰":응답 아약스 호출 내가 아약스 호출을 실행할 때, 나는 오류 다음 얻을

$.ajax({ 
    type: 'GET', 
    url: "http://www.exampleUrl.com", 
    crossDomain: true, 
    dataType: "jsonp", 
    jsonp: "jsonp", 
    mimeType: "application/json", 
    jsonpCallback: 'callback', 
    contentType: "application/json; charset=utf-8", 

    beforeSend: function() { 

    }, 
    success: function (data) 
    {   
     alert("success"); 
    }, 
    error: function (result) { 

    } 
}); 

내 jQuery 코드는 다음과 같습니다

왜 json 응답을받지 못합니까? 내 브라우저에서 동일한 URL을 직접 사용할 때 예상되는 json 응답을 얻습니다. 무엇이 잘못 되었나요?

답변

1

반드시 CORS 문제가 발생합니다.

는 이러한 접근 방식은 완벽하게 작동합니다 :

$.ajax({ 

    // The 'type' property sets the HTTP method. 
    // A value of 'PUT' or 'DELETE' will trigger a preflight request. 
    type: 'GET', 

    // The URL to make the request to. 
    url: 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html', 

    // The 'contentType' property sets the 'Content-Type' header. 
    // The JQuery default for this property is 
    // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger 
    // a preflight. If you set this value to anything other than 
    // application/x-www-form-urlencoded, multipart/form-data, or text/plain, 
    // you will trigger a preflight request. 
    contentType: 'text/plain', 

    xhrFields: { 
    // The 'xhrFields' property sets additional fields on the XMLHttpRequest. 
    // This can be used to set the 'withCredentials' property. 
    // Set the value to 'true' if you'd like to pass cookies to the server. 
    // If this is enabled, your server must respond with the header 
    // 'Access-Control-Allow-Credentials: true'. 
    withCredentials: false 
    }, 

    headers: { 
    // Set any custom headers here. 
    // If you set any non-simple headers, your server must include these 
    // headers in the 'Access-Control-Allow-Headers' response header. 
    }, 

    success: function() { 
    // Here's where you handle a successful response. 
    }, 

    error: function() { 
    // Here's where you handle an error response. 
    // Note that if the error was due to a CORS issue, 
    // this function will still fire, but there won't be any additional 
    // information about the error. 
    } 
}); 

출처 :

+0

Using CORS이 게시물 주셔서 감사합니다. 이제 다음과 같은 응답 오류가 발생합니다. 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 없습니다. 따라서 원본 'null'은 액세스가 허용되지 않습니다. –

+0

이것은 소비하는 서버에서 CORS가 제대로 정의 및 활성화되어 있지 않음을 나타냅니다. 효과적으로 다루는 방법에 대한 자세한 내용은 [enable-CORS] (http://enable-cors.org/server.html)를 확인하십시오. 서버에 액세스 할 수 있기를 바랍니다. – nyedidikeke