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

 

About these ads

Leave a Reply

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

WordPress.com Logo

Komentirate prijavljeni s svojim WordPress.com računom. Log Out / Change )

Twitter picture

Komentirate prijavljeni s svojim Twitter računom. Log Out / Change )

Facebook photo

Komentirate prijavljeni s svojim Facebook računom. Log Out / Change )

Google+ photo

Komentirate prijavljeni s svojim Google+ računom. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: