General discussion

  • Creator
  • #4049139

    Fetch Data


    by trackourshipment512 ·

    I am going to fetch data from my site and display to my another site. But when I put the ajax call it shows nothing in the same server, when when we call the ajax request from different server it shows the result.

    • This topic was modified 1 year, 3 months ago by Avatar photokees_b.

All Comments

  • Author
    • #4049165
      Avatar photo

      Re: fetch data

      by kees_b ·

      In reply to Fetch Data

      On the same server, try localhost to connect to.

    • #4049548

      Reply To: Fetch Data

      by seatyboie ·

      In reply to Fetch Data

      To get around this, utilise Cross-Origin Resource Sharing (CORS) to allow AJAX queries from other domains. CORS is a method that allows a server to control which domains have access to its resources. To enable CORS, configure the server that is supplying the data to include the necessary headers in its response.

    • #4136846

      Reply To: Fetch Data

      by WIlliamhenry ·

      In reply to Fetch Data

      The issue you are facing with the AJAX call might be related to the same-origin policy implemented by web browsers. The same-origin policy restricts web pages from making AJAX requests to a different domain than the one hosting the web page. This is a security measure to prevent unauthorized access to sensitive data and resources.

      When you try to fetch data from your site and display it on another site using AJAX, the browser may block the request due to the same-origin policy, resulting in an empty response.

      However, you mentioned that the AJAX call works when you fetch data from a different server. This indicates that the other server might have enabled Cross-Origin Resource Sharing (CORS). CORS is a mechanism that allows servers to specify which origins are permitted to access their resources. By configuring CORS on the server, you can overcome the same-origin policy and allow AJAX requests from other domains.

      To resolve the issue on your site, you have a few options:

      1. Enable CORS on your server: If you have access to the server hosting the data you want to fetch, you can configure CORS to allow AJAX requests from your other site. This involves adding appropriate CORS headers to the server’s responses.

      2. Use a Proxy Server: You can set up a proxy server on your site that forwards the AJAX request to the original server. Since the request is made from your server (the same origin), there won’t be any same-origin policy issues. The proxy server can then pass the response back to your other site.

      3. JSONP (JSON with Padding): JSONP is a workaround for the same-origin policy that allows you to make cross-domain AJAX requests. However, it requires the server to support JSONP explicitly.

      4. Cross-Origin Embedder Policy (COEP) and Cross-Origin Opener Policy (COOP): These are newer security features that can be implemented to enable cross-origin communication between documents. However, they may have limited support in older browsers.

      Before implementing any of these solutions, ensure that you understand the security implications and take appropriate measures to prevent unauthorized access to your data.

      Remember that CORS should be implemented with care, and you should only allow cross-origin requests from trusted origins to avoid potential security risks. Always verify and sanitize the data received from the server before displaying it on your site to prevent any potential security vulnerabilities.

      • This reply was modified 11 months, 1 week ago by WIlliamhenry.
      • This reply was modified 11 months ago by Avatar photokees_b.
    • #4157311

      Enable CORS on the server.

      by Harisamer214 ·

      In reply to Fetch Data

      To make an AJAX call from the same server work, you need to enable CORS on the server that is receiving the request. This can be done by adding the following headers to the response:

      Access-Control-Allow-Origin: *
      Access-Control-Allow-Methods: GET, POST, PUT, DELETE
      Access-Control-Allow-Headers: Content-Type, X-Requested-With

      If you are using a framework like Django or ASP.NET MVC, there are usually built-in ways to enable CORS. For example, in Django, you can add the following code to your settings file:

      Once you have enabled CORS, your AJAX call should work from the same server.
      Hope this helps !

    • #4169633

      You’re encountering a cross-origin issue with your AJAX call

      by ShellyCraig ·

      In reply to Fetch Data

      You need to enable Cross-Origin Resource Sharing (CORS) on the server you’re making the AJAX request to.

      Ensure that the server allows requests from the domain of your original site. You may need to configure the server’s CORS settings or headers to allow requests from the site where your AJAX call originates.

      This way, your AJAX request should work whether it’s on the same server or a different one.

Viewing 4 reply threads