Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Below is a piece of code in .Net, that reads data from a webservice:
C#
string url = "http://localhost:48677/Service1.svc/GetDataSet1/FENCEGATE";
var webClient = new System.Net.WebClient();
string data = webClient.DownloadString(url);

Now I want to do the same thing in JavaScript, but don't know how to do it. Appreciate if you can share your experience.
Posted

You don't really use a Web service, you are just get HTTP response by URL, not using any of HTTP commands. Are you sure you get the result you want? If this is really way you want, you can do the same using Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^].

One convenient way to use Ajax is using jQuery Ajax: http://api.jquery.com/jquery.ajax/[^].

—SA
 
Share this answer
 
Comments
[no name] 6-Jan-14 8:57am    
SA: I read the article in http://api.jquery.com/jquery.ajax/. Through the URL, the expected results are:
{"Geometry":[{"paths":[[[201394.01178484457,173661.08635829584],[201392.0117168416,173661.08690949593],[201390.01164883861,173661.08746069603],[201388.01158083565,173661.08801189612],[201386.01151283266,173661.08856309619],[201384.0114448297,173661.08911429628],[201382.0113768267,173661.08966549637],...
But in my codee, the 'data' is undefined. I know that the problem is due to the settings (see below) in my code.
$.ajax( {
type:'Get',
url:'http://10.10.22.50:6080/arcgis/rest/services/Test/RailLRSDef/MapServer/exts/SOE_LRSSegment/Segment_Operation?LRSSegInput=A1&MeasureInputA=100&MeasureInputB=1000&f=pjson',
dataType: 'json',
success:function(data) {
debugger;
ajaxData = data;
} error(xhr,status,error) { debugger; }
});
Per your experience, what is wrong in the settings? Besides, How should/can dataFilter be used to remove the {"Geometry":[ , which in the actual result retrieved through the URL? Is it possible to retrieve the data as a string (like my 1st post in thread through .Net's webClient)? Thanks.
Sergey Alexandrovich Kryukov 6-Jan-14 9:17am    
I don't know for sure, did not use it this way, because... did you see that "success" has been deprecated?
The most usual pattern is using .done/.fail/.always:

var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second complete" );
});
(from http://api.jquery.com/jquery.ajax/).
There are more events you can use: deferred.fail, deferred.done, beforeSent, etc...
—SA
[no name] 6-Jan-14 9:32am    
SA: Thanks for your script. Could you hint me the "example.php"? I have never used any php before. Where should I put it. What's the content of the "example.php"? Thanks.
Sergey Alexandrovich Kryukov 6-Jan-14 9:39am    
I don't have time for that, sorry; you can easily experiment with PHP using $_POST: http://www.php.net/manual/en/reserved.variables.post.php.
And are you going to accept my answer formally? I already explained you what to do.
—SA
The best way is use some JavaScript framework - like jQuery...

JavaScript
$.ajax( {
  type:'Get',
  url:'http://localhost:48677/Service1.svc/GetDataSet1/FENCEGATE',
  success:function(data) {
    alert(data);
  }
})


See here for more: http://api.jquery.com/jquery.ajax/[^]
 
Share this answer
 
Comments
[no name] 2-Jan-14 15:06pm    
Thanks for your response. Can I do like that:
var data = $.ajax( {
type:'Get',
url:'http://localhost:48677/Service1.svc/GetDataSet1/FENCEGATE',
success:function(data) {
alert(data);
}
})
Kornfeld Eliyahu Peter 2-Jan-14 15:11pm    
More like this...

var ajaxData;
$.ajax( {
type:'Get',
url:'http://localhost:48677/Service1.svc/GetDataSet1/FENCEGATE',
success:function(data) {
ajaxData = data;
}
})
[no name] 2-Jan-14 15:40pm    
Thanks. Loaded your code, but got error: Microsoft JScript runtime error: '$' is undefined.
However, between <head> </head>, I do load
<script src="~/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
Any hint? Thanks.
Kornfeld Eliyahu Peter 2-Jan-14 15:44pm    
It seems jQuery does not loaded after all. Check your page on the client...
[no name] 2-Jan-14 15:52pm    
The error: Microsoft JScript runtime error: '$' is undefined was gone by change
<script src="~/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
to
<script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900