"Error before before" error for getJSON call

Can anyone explain the following. I’ve been looking for a network for ages, trying to find help, and I believe that I’m doing everything right, but still get 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 web service does not return anything:

 jsonp1301065851157('url/KiN_150x300.jpg'); 

The problem is that when I call GetPageAdvert2 nothing happens.

My WebService (written in VB.Net):

  <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 answer I get (in FF) is this:

 PARAMS: callback jsonp1300979718942 contentType application/json; charset=utf-8 pagename default.html success undefined RESPONSE: jsonp1301065851157('url/KiN_150x300.jpg'); 

This is basically what I think is right.

However, "Warning (data)" produces nothing but "undefined".

+4
source share
2 answers

When using JSONP, you cannot just return a JSON string, since the returned string will actually be entered into the current page in a new script element. You need to return a valid JavaScript statement that calls the callback handler that parses the response.

Here is an example on the Wikipedia page:

 parseResponse({"Name": "Cheeso", Id : 1823, "Rank": 7}) 

Note that the above syntax is not valid JSON, which is not needed in this case, since you only pass a JavaScript object in literal notation.

Also, as x10 comment says, does jQuery change the parameter ? in the callback request parameter to a unique function that you must call, so do not just copy the paste of the above example, but replace parseResponse with the specified parameter.

Update: the last thing you need to change is the jQuery.getJSON parameters: the syntax looks like this:

 jQuery.getJSON( url, [ data ], [ success(data, textStatus, jqXHR) ] ) 

So, you should just pass your success function as a second or third parameter, and not as part of an object (compare this with the generic jQuery.ajax() function call):

 $.getJSON("***URL***.asmx/CheckAdvert?callback=?", function(data) { callback(data) }); 
+3
source

I think your function should look like this:

  function GetPageAdvert2 (url) {
             $ .getJSON ("*** URL HERE ***. asmx / CheckAdvert? callback =?", 
             {
                pagename: url,
                success: function (data) {
                     alert (data);
                }
             });        
         }

Note that success is a property of getJSON. You had it outside

+3
source

Source: https://habr.com/ru/post/1345064/


All Articles