使用jquery.soap调用SOAP XML接口成功但无法获取数据求助
Hey there, let's figure out why you're not getting the return data from your SOAP service call using jquery.soap. I've tackled similar issues before, so here are the key fixes and checks to try:
1. Debug the actual response structure first
Your current code tries to access respuesta.GetSession after converting the response to a string, but that's not the right way to parse the SOAP response. The soapResponse object from jquery.soap has built-in methods to inspect the data properly—let's start by logging the full response to see what we're working with.
2. Fix the response parsing logic
Instead of using toString(), use soapResponse.toJSON() to convert the XML response into a JSON object, which is much easier to navigate. Also, most ASMX SOAP services wrap the result in nested nodes (like GetSessionResponse and GetSessionResult), so you'll need to target those specific paths.
3. Don't forget the namespace
Nearly all ASMX services require a correct namespace in the SOAP request. You can find this namespace in the service's WSDL (look for the targetNamespace value). Add the namespaceURL parameter to your $.soap call to ensure the request is formatted correctly for the service.
Modified working example
Here's an updated version of your code with these fixes and helpful debug logs:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>SOAP Session Test</title> <script src="app/js/jquery.min.js"></script> <script src="app/js/jquery.soap.js"></script> </head> <body> <pre><code id="salida">Waiting for response...</code></pre> <script> $.soap({ url: 'http://myservice.com/sisWSAFI/Service.asmx', method: 'GetSession', // Replace with your actual namespace from the service's WSDL namespaceURL: 'http://your-service-namespace-here/', data: { user: 'user', password: 'pass' }, success: function(soapResponse) { // Log the full response to inspect its structure console.log('Full SOAP Response Object:', soapResponse); // Convert XML response to JSON for easier access const responseJson = soapResponse.toJSON(); console.log('Response as JSON:', responseJson); // Navigate to the actual session data (adjust path based on your service's response) const sessionResult = responseJson.Body?.GetSessionResponse?.GetSessionResult; if (sessionResult) { console.log('Session Data:', sessionResult); // Display the result in your page document.getElementById('salida').textContent = JSON.stringify(sessionResult, null, 2); } else { console.log('Could not locate session data in the response. Check the JSON structure above.'); document.getElementById('salida').textContent = 'Error: Could not parse session data.'; } }, error: function(soapResponse) { console.error('SOAP Request Failed:', soapResponse); document.getElementById('salida').textContent = `Error: ${soapResponse.toString()}`; } }); </script> </body> </html>
Additional checks to try
- Verify the WSDL: Double-check your service's WSDL to confirm the exact structure of requests and responses. This will tell you if parameters need to be wrapped in a specific node, or if the response uses different node names than you expected.
- Test with raw XML: If the JSON data parameter isn't working as expected, try passing a raw XML string as the
datavalue. For example:data: '<GetSession xmlns="http://your-service-namespace-here/"><user>user</user><password>pass</password></GetSession>' - CORS confirmation: If you're testing locally, ensure the SOAP service allows cross-origin requests (CORS). Since you said the connection is successful, this might not be the issue, but it's worth checking if you run into unexpected failures later.
内容的提问来源于stack exchange,提问作者Christian G

