ASP webservice in jQuery

Včasih želite izdelati spletno stran s čimanj nepotrebne solate in obremenjevanja serverja. Če uporabljate ASP.NET webservice prek Scriptamager-ja, potem vam ta samodejno poskrbi za obilo dodatne kode, za lažje klicanje teh funkcij iz javascripta.


Kako klicati webservice ali PageMethod-e s pomočjo jQueryja?


The solution is a bit less intuitive than using the ScriptManager or what you would normally expect from jQuery. Using jQuery’s getJSON() would make sense, but it unfortunately meets neither of the above security criteria.

The most reliable way that I’ve found is to use jQuery.ajax() as follows:

$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "WebService.asmx/WebMethodName", data: "{}", dataType: "json" });

As you can see, the first parameters set the request type and content-type as required by the ASP.NET AJAX framework.

When making a read-only request, the empty data parameter is the key. For reasons unknown, jQuery does not properly set the specified content-type when there is no data included.

The “{}” data parameter represents an empty JSON object, which you can think of as the JSON equivalent of null. Supplying this parameter will convince jQuery to correctly send the content-type that we’ve supplied, while it will be safely ignored by the web service.




Več preberite v Using jQuery to Consume ASP.NET JSON Web Services



Kaj pa ti misliš o taki izdelavi spletnih strani?Publish



Oddajte komentar

Fill in your details below or click an icon to log in: Logo

Komentirate prijavljeni s svojim računom. Odjava /  Spremeni )

Google+ photo

Komentirate prijavljeni s svojim Google+ računom. Odjava /  Spremeni )

Twitter picture

Komentirate prijavljeni s svojim Twitter računom. Odjava /  Spremeni )

Facebook photo

Komentirate prijavljeni s svojim Facebook računom. Odjava /  Spremeni )


Connecting to %s

%d bloggers like this: