Solved

How can I resolve this web service issue?

Posted on 2013-02-04
6
224 Views
Last Modified: 2013-02-06
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
Comment
Question by:Loganathan Natarajan
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38851230
Shouldn't that be GET, as you pass an id with an address ? ?
 type : "GET",
0
 
LVL 33

Expert Comment

by:Slick812
ID: 38853474
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
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 38853654
@  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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 38856403
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
 
LVL 36

Author Comment

by:Loganathan Natarajan
ID: 38858076
@ Slick812, thanks
0
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 38862522
thanks
0

Featured Post

Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

Join & Write a Comment

Steve Terp was featured in a video created by CRN about how "Channel Is Crucial To Market Disruption". Click on View source to see the video and article
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now