views:

52

answers:

3

Hi there,

I am trying posting a data to a web service. And this service in a different project in same solution. This project name is WebServices and web service's name is HastaTahlilUyariService.asmx. My code is here:

 $.ajax(
                {
                    type: "POST",
                    url: "WebServices/HastaTahlilUyariService.asmx/f_HastaninAktarilacakAlislabTestleri",
                    data: "{_sTcKimlikNo:" + Cell.innerHTML + ",_iKlinikKodu:18001,_bAy:12,_iYil:2009}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        alert(msg);
                    },
                    error: function(msg) {
                        alert( 'hata'+ msg);
                    }
                }
                );

I think my url is wrong. How can i give correct url.

Thanks in advance...

A: 

First of all: build your webservice, and configure it to run under something like http://localhost/services/myservice.asmx in IIS Configuration Manager. Open IIS Manager, rightclick on website -> New Virtual Directory; and navigate to the folder where your webservice is located. Name the virtual directory then services.

Then call the service with it's fully qualified url like http://localhost/myservice.asmx/function.

The most easy way to do what you are trying to do (well, I guess)

Create the method you want to call in your codebehind like

[WebMethod]
public static object MethodToCallFromAjax(string argument)
{
   //do something
   return result;
}

Then add a ScriptManager to your aspx page; and set enablePageMethods=true. Then call your method from JavaScript like:

PageMethods.MethodToCallFromAjax("argument value", function(msg) { alert(msg); });

edit: removed some stuff about json and asmx that wasn't true :-)

Jan Jongboom
no, it is perfectly possible to consume asmx webservices through jquery. i do that all the time, and i'm not quite unique when it comes to that, either.
David Hedlund
edited question to reflect the above
Jan Jongboom
jQuery is more efficient than the built-in library that comes with asp.net. I'd suggest using it instead.
Ariel
efficient != most easy
Jan Jongboom
A: 

You might want to change your url to something that isn't relative to where you are right now, such as url: '/WebServices/... (initial slash...)

Using the firebug addon for firefox, you can inspect the AJAX callback and see what exact URL is being requested. You can copy that URL, and you should be able to point your browser to the URL of the asmx (i.e. without the last parameter, which is the method name).

Other than that, you need to make sure you've uncommented the ScriptService attribute in the top few lines of the asmx code file. It is commented out by default, but it needs to be there to allow jQuery to access the webservice.

David Hedlund
A: 

Another option to look into is the standard XMLHttpRequest object that is built into the browser (for IE 6 you have to use the ActiveX object with the same name). It makes calling XML services pretty easy, although you end up having to some of the SOAP formatting yourself.

Wikipedia entry for XMLHttpRequest

Peter Jacoby