Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How can I resolve this web service issue?

I am trying to access the API url of my external client. From there I need to populate the data based on emp_id from the API webservice. while I try from my website, it says the below error.

XMLHttpRequest cannot load http://test.com/apis/sxmobile/globalapsi?emp_id=xxx. Origin http://mywebsite.com is not allowed by Access-Control-Allow-Origin.

Open in new window


Is there a way to fix ? please help me.

my ajax code,

<script>
var url_path = "http://test.com/apis/sxmobile/globalapsi?emp_id=xxx";
$.ajax({  
           type : "POST",  
           url : url_path,  
           dataType : 'text',
          // async : true,
           success :function(data){
                               alert(data);
               //eval('var data = ' + data + ';');
             // if(data != null && data != -1)
              //alert("name - " + data.name);

           } ,
           error : function(data, textStatus, errorThrown) {
               alert("error" + errorThrown);
           }
       });
</script>
 

Open in new window

0
Loganathan Natarajan
Asked:
Loganathan Natarajan
  • 3
  • 2
1 Solution
 
Lukasz ChmielewskiCommented:
Shouldn't that be GET, as you pass an id with an address ? ?
 type : "GET",
0
 
Slick812Commented:
greetings  logudotcom, , difficult to judge from your description, but the error message includes -

Origin http://mywebsite.com is not allowed by Access-Control-Allow-Origin.

I think that most web browsers, limit the web access points of AJAX requests to the DOMAIN of the containing web page, so I would take it that the viewed page is from -
http://mywebsite.com

so ajax can not get something from "http://test.com"
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
@  Roads_Roads, thanks  but still i get same error if i put GET method.

@ Slick812, thanks,  yes, i think it might be cross domain access issue.  even i tried cURL way also.

Is there any way to achieve this OR we need to conclude that we cannot access anyway that API url from other ways.?
0
2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

 
Slick812Commented:
I seems that for ajax returns to succeed, you will have have a web address in your own DOMAIN, I have seen some examples in PHP scripts, that can access any web address (with  file_get_contents(),  fopen ( ) ,  cURL, etc. ) and get the returned data (web-page, xml , image,  json ) and then echo it back to the ajax return.
But this can be trouble-some, since the type of data returned from some XML and JSON access, can change, or return a different type-data or some ERROR or other.
I guess that's what you mean here?
although you say = "even i tried cURL way also"
which seems like you tried something before?
you really have to do the test of the access for cURL in a regular PHP page, thats NOT an AJAX return, and just display what you get back from the cURL, and when you configure cURL correctly to get the correct data from cURL, then you can move it to the AJAX return page. At least that's the way I would do it.
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
@ Slick812, thanks
0
 
Loganathan NatarajanLAMP DeveloperAuthor Commented:
thanks
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now