Search code examples
javascriptphpcurlcall

Is there any way to call JavaScript API call as we can have PHP curl call?


I need data from different website with using of RESTapi call. I have one PHP file which is using curl and giving me data with RESTapi call. But here I wanted solution is that, I don't want to use any PHP file or Curl call, just wanted to RESTapi call with using JavaScript or Ajax Or jQuery. Is there any way to call like this?


Solution

  •     <script language="javascript">
    function test()
    {    
    var request = new XMLHttpRequest();
        var path="http://api.crossMRKT.net/rest/v1/{user_name}/list";
        request.onreadystatechange=state_change;
    
        request.open("GET", path, true);
        request.setRequestHeader("Referer", "http://www.google.com");
        request.setRequestHeader("X-APikey","12345aaaaa67890aaaaa");
        request.setRequestHeader("Content-Type","application/json");
        request.setRequestHeader("Access-Control-Allow-Origin","*");
    
        request.send(null);
    }
            function state_change()
        {
        if (request.readyState==4)
          {// 4 = "loaded"
          if (request.status==200)
            {// 200 = OK
                      console.log(response);
            }
          else
            {
            alert("Problem ...");
            }
          }
        }
        </script>
    

    call this function on any button click or any event you want to attach and you should be through. Also make sure you supply your username in this line
    var path="http://api.crossMRKT.net/rest/v1/{user_name}/list";