Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I resolve this web service issue?

Posted on 2013-02-04
6
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 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

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

722 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