I have a webmethod hosted on domain A inside a .aspx page. I need to call this method from domain B. I am using $getJSON() to make this call, but when it comes back data.query.results[0] is always null.
Jquery code in domain B.
// Accepts a url and a callback function to run.
function requestCrossDomain() {
var site = 'http://domain A/Services/A.aspx/GetString';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=json&callback=?';
$.getJSON(yql, cbFunc);
function cbFunc(data) {
if (data.query.results[0]) {
var result = JSON.parse(data.results[0]);
alert(result);
}
else
throw new Error('Nothing returned from getJSON.');
}
}
Domain A Webmethod
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)>
Public Shared Function GetString() As String
Dim ResultString As String = New JavaScriptSerializer().Serialize("Test Result")
Return ResultString
End Function
The above method works absolutely fine when I call the method from the same domain. But cross domain call always returns null.
When I checked in Fiddler, there is track that says the server responded and the content length is 616. But the result I get back is null.
Response Headers
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcQ29tbW9uXFBST1xIUk9cU2VydmljZXNcSFJPQ29yZS5hc3B4?=
X-Powered-By: ASP.NET
Date: Mon, 05 Dec 2011 15:07:54 GMT
Content-Length: 616
I am not sure what I am missing. I’ve tried using JSONP and $getJSON. Both doesn’t return any value.
Please let me know if you require any further information.
ok, I see two problems with the script. First, you should create the
cbFuncbefore passing it into thegetJSON. Secondly lets check if yahoo is returning it’s data by alerting when it was created. If the results is empty you’ll need help with the yahoo api more.