0

Hi i tried to call a rest webservice from ajax get request.

Following is the code i tried for that.

function callRestService(){
        var xmlhttp;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        $.ajax({
                xmlhttp.open("GET","http://mywebservice/test/",false);
                xmlhttp.send();
                alert(xmlhttp.responseText);
        });
    }

And following error am getting while running this code/

missing : after property id
[Break On This Error]   

xmlhttp.open("GET","http://mywebservice/test..

/AjaxC...ervice/ (line 30, col 13)

In the first case i tried something like following

$.ajax({
              type: "GET",
              url: "http://mywebservice/test/",
              cache: false,
              success: function(data){
                    alert(data);
                    //var obj = eval(data);
              },
               error: function (msg, url, line) {
                   alert('error trapped in error: function(msg, url, line)');
                   alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
               }
        });

and in the above case control comes into the error block, but i didn't get what's the reason for that .and that's why i tried the first case.

Is there any problem with this code..??Can anyone help in this.?

TKV
  • 2,325
  • 9
  • 39
  • 54

1 Answers1

0

Your code is all wrong. Assuming that$.ajax is jQuery ajax call, then Your code should look like this:

function CallRestService() {
  $.ajax({url:'http://mywebservice/test'}).done(function(data) {
      alert(data);
   })
  );
}

You don't need to create xml http request if you're using jquery ajax call. See this: http://api.jquery.com/jQuery.ajax for reference.

If you don't want to use jQuery:

function callRestService(){
    var xmlhttp;
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","http://mywebservice/test/",false);
    xmlhttp.send();
    if (xmlhttp.status == "200") {
        alert(xmlhttp.responseText);
    }
}

Reference here: Using XMLHttpRequest

If you are using cross domain calls, see here: jQuery AJAX cross domain

beaver
  • 485
  • 9
  • 16
WojtekT
  • 4,625
  • 23
  • 37
  • yes. i know , but that cause error for me and thats why i test like this.i will post what i tried with ajax itself. – TKV May 04 '12 at 09:23
  • i tried your updated code also .in that case am getting the following error..Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [Break On This Error] xmlhttp.send(); – TKV May 04 '12 at 09:35
  • As to your error, see here: http://stackoverflow.com/questions/2060551/xmlhttprequest-to-get-http-response-from-remote-host – WojtekT May 04 '12 at 09:39
  • yes..these are 2 applications..am trying to get response from another server – TKV May 04 '12 at 09:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10881/discussion-between-tijo-k-varghese-and-wojtekt) – TKV May 04 '12 at 09:51
  • ok. in that case i got an exception like the following. msg = [object Object], url = parsererror, line = jQuery16005708947525814101_1336125811693 was not called – TKV May 04 '12 at 10:06