views:

912

answers:

9

I have read a lot about jquery and i have a webservice where i convert a companyID to the real companyName. Now i want to call that webservice with jquery or javascript. The webservice is on host http://webservice/service.asmx en i'm working on http://tlmos. I don't work and i always get an error

Here is my code:

<script type="text/javascript" src="http://kmosvi24/_layouts/jquery-1.3.2.min.js"&gt;&lt;/script&gt;

<script type="text/javascript"> 
var test = "KBEACDNV";
$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "http://webservice/service.asmx/getCompanyByCompanyID",
  data: "{'sCompanyID:' + 'test'}",              
  dataType: "json",
  succes:function(response){        alert("good");    },
  error: function(response) { alert("Uh oh"); },
 complete: function(response) {        alert("" + response);    }
});

</script>

Can someone help me?

thx

+6  A: 

Umm.. you spelled success wrong on line 11

.. and you probably want to format your data as

data: "sCompanyID=test"

Take a quick pass through the jQuery API page on this one to verify you are passing the parameters that your service expects. It looks like you are expecting a SOAP packet with an ASMX service, and jQuery is more suited to hitting a restful service generated from an ASHX file or WCF service.

Jeff Fritz
A: 

In order to run your web-services from Jquery, you should use either WCF or just usual web services, but you should add [ScriptMethod] to your service's method and [ScriptService] to your webservice description.

Wow wow just noticed that you're trying to call the service from one host to another... that one won't work. service should be hosted on the same domain as the page where it's being called from.

as a reply to Jeff's answer, correct way to format data is data: {key: "value"}

ifesdjeen
Agreed about formatting data, I did not notice that the datatype was JSON.
Jeff Fritz
+1  A: 

I don't think you are using the data parameter right, usually it's a key-value pair like:

data: {sCompanyID: 'test'}

I believe that they way you are using it will result in jQuery attempting to post to http://webservice/service.asmx/getCompanyByCompanyID?sCompanyID:blah

Also aren't .NET web services SOAP? I don't think jQuery can parse that...

edit: Nevermind, didn't realize you were passing these as json data. Thanks commenters!

matt b
the data is being passed correctly, it's expecting a json string, not a javascript variable.
John Boker
jquery will auto encode it to a query string. You can send a JSON string to a asmx method that has [ScriptService] and [ScriptMethod]
Chad Grant
Ah didn't realize it could do that! Updated answer.
matt b
+1  A: 

You can't do AJAX calls to hosts other than your own. If you really have to do this make a call to your own server and use a simple proxy to redirect to the domain you need.

You could do this for example by using a ProxyPass-directive in your webserver:

ProxyPass         /service/ http://webservice/service.asmx
ProxyPassReverse  /service/ http://webservice/service.asmx

Then you can issue an AJAX-request to /service/getCompanyByCompanyID and it will be proxied to the correct URL.

Benedikt Eger
where do i add this in to my script?
idsis
This is an Apache-configuration option. You can't achieve this by just altering the client-side. You need a proxy on the server in your own domain.
Benedikt Eger
+2  A: 

As some othe people have pointed out you cannot call a webservice on another domain, however as you are using ASP.NET, you can write a raw HTTP handler (normally with an .ashx extension to proxy your request from client to server.) Which you'd place on your "timos" server

so in your ashx file you can write something along the lines of...

public void ProcessRequest (HttpContext context)
{

    XmlDocument wsResponse = new XmlDocument();
    string url =  "http://webservice/service.asmx/getCompanyByCompanyID?CompanyID="
    context.Request.Form["CompanyID"].ToString()
    wsResponse.Load(url);
    string XMLDocument = wsResponse.InnerXml;        
    context.Response.ContentType = "text/xml";        
    context.Response.Write(XMLDocument);

}

Hope this helps.

ChrisV
+2  A: 

You can make a request to a different server, but only if the call uses GET. Since all you do is lookup anyway, a GET request should be fine.

tomjen
I would note that you can only process the result if the result is a JS function.
altCognito
A: 

so if i get it right i can't call my webservice without changing the source code of it?

idsis
A: 

when i test my webservice i get a xml file. And when i call the service with jqeury i always get the error and not the success

Please help me

idsis
A: 

With jQuery Ajax Requests you need to use the following format when defining the variables to be sent in the request:

data: "variableName=variableContent",

You wrote:

 data: "{'sCompanyID:' + 'test'}"

This won't work for a two reasons:
- You have included curly brackets which don't need to be there.
- You have used a semi-colon,":", instead of an equals sign,"=".

So long as you change these it should work.

P.S I only just realized that Jeff Fritz has already given you the right answer. His answer is spot on!

The_Lorax