jQuery ajax GET returns 405 Method Not Allowed

前端 未结 2 1788
死守一世寂寞
死守一世寂寞 2020-12-11 23:59

I am trying to access an API that requires authentication. This is the code that i am using but i keep getting a 405 Method Not Allowed error. Any thoughts? (my username a

相关标签:
2条回答
  • 2020-12-12 00:41

    You can't make javascript/Ajax calls across domains (without some serious kludging). What we've done for this in the past is to create a local jsp proxy that we call with our javascript but is just a pass-through to the remote URL.

    Here is some example JSP code that is close to what I've used to hit a SOLR instance returning JSON.

    final String ENCODING = "UTF-8"; // this is the default unless specified otherwise (in server.xml for Tomcat)
        // see http://tomcat.apache.org/tomcat-6.0-doc/config/http.html#Common_Attributes and
        // http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q2
        final String URI_ENCODING = "ISO-8859-1";
    
        HashMap<String, String> defaultSettings = new HashMap<String, String>() {
            {
                put("wt", "json");
                put("rows", "10");
                put("start", "0");
            }
        };
    
        BufferedReader searchResponse = null;
        OutputStreamWriter forwardedSearchRequest = null;
    
        // TODO:  are there any headers that we need to pass on?
        // simply pass on the request to the search server and return any results
        try {
            URL searchURL = new URL("http://yourdestinationurlhere.com");
            HttpURLConnection conn = (HttpURLConnection) searchURL.openConnection();
    
            // read the request data and send it as POST data (unchanged)
            conn.setDoOutput(true);
            forwardedSearchRequest = new OutputStreamWriter(conn.getOutputStream());
    
            // at least for Tomcat 6.0, the default is really iso-8859-1, although it is reported as UTF-8
            // so, we will explicitly set it to URI_ENCODING in both places
            request.setCharacterEncoding(URI_ENCODING);
    
            String query = (String) request.getParameter("q");
            if ((query != null) && (! "".equals(query.trim()))) {
                query = URLEncoder.encode(query, request.getCharacterEncoding()); // we must use the same setting as the container for URI-encoding
                forwardedSearchRequest.write("&q=");
                forwardedSearchRequest.write(query);
            } else {
                // empty queries may return all results, so let's circumvent that
                forwardedSearchRequest.write("&q=help");
            }
    
            for(String key:defaultSettings.keySet()) {
                String resultType = (String) request.getParameter(key);
                if ((resultType == null) || "".equals(resultType.trim())) resultType = defaultSettings.get(key);
                forwardedSearchRequest.write("&"+key+"=");
                forwardedSearchRequest.write(resultType);
            }
    
            forwardedSearchRequest.flush();
    
            // read and forward the response
            // reset anything that may have been sent so far
            out.clearBuffer();
    
            // do this only if we have a 200 response code
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // the web server may be running as windows-1252, so let's ensure we have the right CS
                searchResponse = new BufferedReader(new InputStreamReader(conn.getInputStream(), ENCODING));
    
                String contentType = conn.getHeaderField("Content-Type");
                if ((contentType != null) && (! "".equals(contentType))) response.setHeader("Content-Type", contentType);
    
                String buffer;
                while ((buffer = searchResponse.readLine()) != null) out.println(buffer);
            } else {
                // dish out a mock-Solr-JSON response that includes a status and an error
                response.setHeader("Content-Type", "application/json");
                out.println("{ responseHeader: {status: -1, responseCode: " + conn.getResponseCode()  +
                    ", responseMessage: \"" + conn.getResponseMessage() + "\" } }");
            }
        } catch (Exception e) {
            throw new ServletException("Exception - " + e.getClass().getName(), e);
        } finally {
            if (forwardedSearchRequest != null) forwardedSearchRequest.close();
            if (searchResponse != null) searchResponse.close();
        }
    
    0 讨论(0)
  • 2020-12-12 00:42

    try to change your datatype to "jsonp" is you are trying to send a cross domain request

    0 讨论(0)
提交回复
热议问题