Can someone please explain the following. I've scoured the net for ages trying to find help and I believe I'm doing everything correctly but still getting errors.
I have the following script on my page:
function GetPageAdvert2(url) {
$.getJSON('http://url/servicename.asmx/CheckAdvert?callback=?',
{
pagename: url,
success: function(data) { alert(data) }
});
};
And my webservice returns nothing more than:
jsonp1301065851157('url/KiN_150x300.jpg');
The problem is that when I call GetPageAdvert2, nothing is happening.
My WebService (written in VB.Net) is:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False, UseHttpGet:=True)> _
Public Sub CheckAdvert(ByVal pagename As String, ByVal callback As String)
Dim pageUID As Integer = 0
Dim advertURL As List(Of aU) = New List(Of aU)()
Dim sss As String
Using con As New System.Data.SqlClient.SqlConnection(My.Settings.sqlConnection2)
SQL STUFF IN HERE
the SELECT statement will return a value and place it in Variable SSS
End Using
Context.Response.ContentType = "application/json"
Context.Response.Write(callback & "('" & sss & "');")
Context.Response.End()
End Function
The response I'm getting back (in FF) is:
PARAMS:
callback jsonp1300979718942
contentType application/json; charset=utf-8
pagename default.html
success undefined
RESPONSE:
jsonp1301065851157('url/KiN_150x300.jpg');
This is mostly, what I believe, to be correct.
However the "Alert(data)" isn't producing anything other than "undefined".
Can someone please explain the following. I've scoured the net for ages trying to find help and I believe I'm doing everything correctly but still getting errors.
I have the following script on my page:
function GetPageAdvert2(url) {
$.getJSON('http://url/servicename.asmx/CheckAdvert?callback=?',
{
pagename: url,
success: function(data) { alert(data) }
});
};
And my webservice returns nothing more than:
jsonp1301065851157('url/KiN_150x300.jpg');
The problem is that when I call GetPageAdvert2, nothing is happening.
My WebService (written in VB.Net) is:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False, UseHttpGet:=True)> _
Public Sub CheckAdvert(ByVal pagename As String, ByVal callback As String)
Dim pageUID As Integer = 0
Dim advertURL As List(Of aU) = New List(Of aU)()
Dim sss As String
Using con As New System.Data.SqlClient.SqlConnection(My.Settings.sqlConnection2)
SQL STUFF IN HERE
the SELECT statement will return a value and place it in Variable SSS
End Using
Context.Response.ContentType = "application/json"
Context.Response.Write(callback & "('" & sss & "');")
Context.Response.End()
End Function
The response I'm getting back (in FF) is:
PARAMS:
callback jsonp1300979718942
contentType application/json; charset=utf-8
pagename default.html
success undefined
RESPONSE:
jsonp1301065851157('url/KiN_150x300.jpg');
This is mostly, what I believe, to be correct.
However the "Alert(data)" isn't producing anything other than "undefined".
Share Improve this question edited Mar 25, 2011 at 15:25 JasonMHirst asked Mar 24, 2011 at 10:59 JasonMHirstJasonMHirst 5765 gold badges13 silver badges31 bronze badges 14- JSON should start and end with curly braces: {"url":"KiN_150x300.jpg"} – Oscar Commented Mar 24, 2011 at 11:09
- 1 @Oscar: that's not true: in this case, the server returns an object wrapped in an array. – Marcel Korpel Commented Mar 24, 2011 at 11:11
-
@Oscar: Incorrect. That is if you only want to return a single object. If you want to return an array you start with surround it with
[]
. – Alxandr Commented Mar 24, 2011 at 11:13 -
Ok, now you need to replace
test
with thecallback
parameter. If you define afunction test(a) { alert(a); }
now, does it alert something like[object Object]
? – Marcel Korpel Commented Mar 24, 2011 at 15:17 -
1
No, the indenting is meant for this site, not for your code on the server. The problem is VB related: you should probably just "print" the string
jsonp1301039560625([{"url":"url/KiN_150x300.jpg"}])
to the client, you shouldn't output an XML document, as the server does at the moment. – Marcel Korpel Commented Mar 25, 2011 at 10:46
2 Answers
Reset to default 3When using JSONP, you can't simply return a JSON string, as the string returned will actually get injected into the current page in a new script
element. You need to return a valid JavaScript statement that calls a callback handler, in which the response is parsed.
There's an example on the Wikipedia page:
parseResponse({"Name": "Cheeso", Id : 1823, "Rank": 7})
Note that above syntax is not valid JSON, which is in this case not needed, as you only pass a JavaScript object in literal notation.
Moreover, as x10 said in his ment, jQuery changes the ?
in the callback
query parameter to a unique function, which you should call, so don't just copy-paste the above example, but replace parseResponse
with the mentioned parameter.
Update: the final thing you should change are the parameters to jQuery.getJSON
: the syntax looks like:
jQuery.getJSON( url, [ data ], [ success(data, textStatus, jqXHR) ] )
So you should just pass your success function as the second or third parameter, not as part of an object (pare this to the general jQuery.ajax()
function call):
$.getJSON("***URL***.asmx/CheckAdvert?callback=?", function(data) { callback(data) });
I think your function should look like:
function GetPageAdvert2(url) { $.getJSON("***URL HERE***.asmx/CheckAdvert?callback=?", { pagename: url, success: function(data){ alert(data); } }); }
Note that the success is a property of the getJSON. You had it outside