Calling an ASMX Web Service through JQuery .Ajax
-
The following is my HTML page. My problem is with invokeService(). It correctly determines the body and places it in data - but then it never sends the data. Fiddler shows no JSON data was sent. The Success routine is called but the data is null (as would be expected with no input data). Can anyone tell me what I am missing or did wrong. How do I send the JSON to the service? Thanks Brent
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Voter Info</title>
<script type="text/javascript" src="jquery-1.4.1.js"></script><script type="text/javascript"> function invokeService() { var value = document.getElementById("voter").value; var loc = "https://www.voterfocus.com/mais/asmx/service1.asmx"; var body = "{'FVRSVoterIDNumber':'" + value + "', 'County':'lee', 'CKey':'------'}"; input.innerText = body; $.ajax({ type: "POST", url: loc + "/GetFVRSVoter", data: body, contentType: "application/json; charset=utf-8", dataType: "json", success: ajaxSucceeded, error: ajaxFailed }); } function ajaxFailed(data) { result.innerText = data.status + ' ' + data.statusText; } function ajaxSucceeded(data) { if (data != null) result.innerText = data.d; else result.innerText = data; } </script>
</head>
<body>One Voter: <input type="text" id="voter" /> <input type="button" onclick="invokeService();" value="GO" /> Input Body: Raw Result:
</body>
</html>Brent
-
The following is my HTML page. My problem is with invokeService(). It correctly determines the body and places it in data - but then it never sends the data. Fiddler shows no JSON data was sent. The Success routine is called but the data is null (as would be expected with no input data). Can anyone tell me what I am missing or did wrong. How do I send the JSON to the service? Thanks Brent
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Voter Info</title>
<script type="text/javascript" src="jquery-1.4.1.js"></script><script type="text/javascript"> function invokeService() { var value = document.getElementById("voter").value; var loc = "https://www.voterfocus.com/mais/asmx/service1.asmx"; var body = "{'FVRSVoterIDNumber':'" + value + "', 'County':'lee', 'CKey':'------'}"; input.innerText = body; $.ajax({ type: "POST", url: loc + "/GetFVRSVoter", data: body, contentType: "application/json; charset=utf-8", dataType: "json", success: ajaxSucceeded, error: ajaxFailed }); } function ajaxFailed(data) { result.innerText = data.status + ' ' + data.statusText; } function ajaxSucceeded(data) { if (data != null) result.innerText = data.d; else result.innerText = data; } </script>
</head>
<body>One Voter: <input type="text" id="voter" /> <input type="button" onclick="invokeService();" value="GO" /> Input Body: Raw Result:
</body>
</html>Brent
-
If it hits the success function that tells me it called your service correctly. Did you put a breakpoint in your webmethod?
There are only 10 types of people in the world, those who understand binary and those who don't.
-
The service is not mine, unfortunately, but, like I said, Fiddler confirms that no data was sent. Is there something I need to do to prepare the data to be sent?
Brent
-
What is the signature for your GetFVRSVoter() method?
There are only 10 types of people in the world, those who understand binary and those who don't.
I'm wondering if I'm wasting my time here. History: Our vendor created this service and also gave us a web site where everything works, but he never gave us access to the WSDL. My employer wanted it to do something different, so asked me to extract the functionality and use it in our own web site. So I don't even have a signature, I am just copying out what is in the vendor's JavaScript file. Is doing something like that even possible? Or am I running in to cross-site scripting problems? This could be something entirely different. But when I run the same script in the vendor's web page, Fiddler shows the data being sent and so when mine gets called and no data is sent, I am just hoping it is me doing something wrong instead of me trying to do something impossible. Thanks
Brent
-
I'm wondering if I'm wasting my time here. History: Our vendor created this service and also gave us a web site where everything works, but he never gave us access to the WSDL. My employer wanted it to do something different, so asked me to extract the functionality and use it in our own web site. So I don't even have a signature, I am just copying out what is in the vendor's JavaScript file. Is doing something like that even possible? Or am I running in to cross-site scripting problems? This could be something entirely different. But when I run the same script in the vendor's web page, Fiddler shows the data being sent and so when mine gets called and no data is sent, I am just hoping it is me doing something wrong instead of me trying to do something impossible. Thanks
Brent
-
Thanks for the additional info. It is possible you are having cross-site issues; however, I believe the error function is called when that happens.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
It may be a proxy authentication problem. I managed to get it working in c#, but had to use proxy authentication. How would I do proxy authentication for this in javascript?
Brent
dbrenth wrote:
How would I do proxy authentication for this in javascript?
Not sure. I haven't had to do that in JS but here's the documentation for .ajax, http://api.jquery.com/jquery.ajax/[^]. If it can be done it would be using one of those properties, perhaps the headers.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
I'm wondering if I'm wasting my time here. History: Our vendor created this service and also gave us a web site where everything works, but he never gave us access to the WSDL. My employer wanted it to do something different, so asked me to extract the functionality and use it in our own web site. So I don't even have a signature, I am just copying out what is in the vendor's JavaScript file. Is doing something like that even possible? Or am I running in to cross-site scripting problems? This could be something entirely different. But when I run the same script in the vendor's web page, Fiddler shows the data being sent and so when mine gets called and no data is sent, I am just hoping it is me doing something wrong instead of me trying to do something impossible. Thanks
Brent
dbrenth wrote:
Or am I running in to cross-site scripting problems?
That's the most likely issue - cross-domain AJAX requests won't work without making changes to the server you're calling. You would either need to change the service to use JSONP[^], or set the
Access-Control-Allow-Origin
header[^] on the server to allow calls from your site. If you don't have access to the server that's hosting the service, there's nothing you can do from the client side. You'd need to write your own service to proxy calls to the remote service for your site. The client-side code would call your service; your service would then call the original service, and return the results to the client.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer