0

Sorry for asking this question. I know this is a common issue but i am not able to find the solution.

I am trying to send a xmlhttprequest to a server(different domain) for invoking rest service,

What I tried is:

ad1,city,sp,pc,cn will taken by getElementbyID.

var url='http://*******:***/rest/code/results.json'+'?AddressLine1='+ad1+'&City='+city+'&StateProvince='+sp+'&PostalCode='+pc+'&Country='+cn; 

 var req;
    if (typeof XMLHttpRequest != "undefined")
     {
     req = new XMLHttpRequest();
    }
    else
    {
     try
     {
         req = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
         req = new ActiveXObject("Microsoft.XHTTP");
     }
 }

 // populate the request, username and password

 var userName ='admin';
 var password = 'admin';

 //Execute the REST request against a server secured with Basic Authentication

 req.open("GET",url,true,userName,password); 
 req.send();

 //Display the response

 var resp = req.response;
alert(resp);}

output what I am getting is an alert with no data.

In web browser console , it is showing as

Error 1: "url(Server rest service url with input parameters) 401 (Unauthorized) " and

Error 2: XMLHttpRequest cannot load url(Server rest service url) Origin http://**:8081(My machine IP) is not allowed by Access-Control-Allow-Origin.

If i opened the url(Server rest service url with input parameters) in new tab, i am getting the output.

Any help is greatly appreciated ..

Thank you.

Arun
  • 15
  • 9

1 Answers1

0

You'd need to modify your GET request to be a JSONP request.

How to make a JSONP request from Javascript without JQuery?

Community
  • 1
  • 1
Tamas
  • 10,953
  • 13
  • 47
  • 77
  • sorry, still i am getting same error.. Another error"url(Server rest service url with input parameters) 401 (Unauthorized) ". How to resolve this?? – Arun Jul 15 '13 at 05:37
  • can you update your question with the HTTP dump of your request so we can see what's going on? – Tamas Jul 15 '13 at 06:57
  • Thanks fine, but we'll need to see the HTTP request (use Firebug or Chrome Dev Tools) - this way we can see what is going on behind the scenes and why you're receiving the 401. – Tamas Jul 15 '13 at 08:22