Reputation: 3109
I'm making the following call in jQuery, using jsonp as my data format, that I'd like to make directly in Silverlight:
$.getJSON('https://api.wordstream.com/authentication/login?callback=?',
{ 'username': un,
'password': pw
}, function (loginResults) {
API_SESSION_ID = loginResults['data']['session_id'];
$.getJSON('https://api.wordstream.com/keywordtool/get_keywords?callback=?',
{ 'session_id': API_SESSION_ID,
'seeds': keyword,
'max_results': 20
}, function (keywordResults) {
for (i = 0; i < +keywordResults['data'].length; i++) {
Keywords[i] = keywordResults['data'][i][0];
}
return(Keywords);
});
});
I tried something like this to handle the first $.getJSON (authenticating & returning my auth token):
WebClient downloader = new WebClient();
WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);
var client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UseDefaultCredentials = false;
client.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(loginEndpoint);
When I try and run this I get the following error inside my downloadstringcompleted eventhandler:
{System.Security.SecurityException: Security error.
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)}
I've used WCF Ria Services in EF & SOAP services via .asmx files in the past, so I'm not a total stranger to the idea of web services. I am not sure if I need to be using the clientaccesspolicy or crossdomain files or not.
Any ideas on how to proceed?
Thanks, Scott
Upvotes: 1
Views: 270
Reputation: 75093
Did a little digging and a test.
When you are calling an external domain, the cross-domain issue will occur and that's why you are seeing the Security error
.
Remember that this is a Web application after all, it does run inside the browser!
To enable Silverlight to reach outside it's domain, give this article a try if you are doing a self hosted app.
Upvotes: 1
Reputation: 189457
What you trying to do in this series of questions has become clearer to me now.
Unless api.wordstream.com
includes a ClientAccessPolicy xml (or the Flash equivalent) you will not be able to make requests to this api from Silverlight.
You have two options:
I would recommend the first approach, however don't use getJSON
. Instead use the standard ajax
api in JQuery
to fetch the JSON content asynchronously. When the final JSON content is available (still in string form) call into Silverlight from Javasript passing in the string.
What would be preferable is to create the appropriate set of .NET classes and collections that match the data from the api. You could then use DataContractJsonSerialializer
to deserialize the received string into instances of your classes.
Sometimes creating a class structure can be a bit of a burden. Another approach is to use the set of objects in the System.Json
namespace starting with JsonValue.Parse
to load up the set of JsonObjects from the string. You can now navigate around the returned data using these `son objects and Linq where necessary.
Upvotes: 1