简体   繁体   English

从jQuery调用WCF服务有什么问题?

[英]What is wrong with this call to a WCF service from jQuery?

This function is wired up to the click event of a button: 此功能连接到按钮的单击事件:

function BlahBlahBlahWCFXML() { 
    varType = "POST"; 
    varUrl = "http://123.123.123.123/NameOfService.svc/GetStuffById"; 
    varData = '{"stuffId": "' + 12345678-abcd-9012-3456-abcdefghjkl' + '"}'; 
    varContentType = "application/json; charset=utf-8";  
    varDataType = "xml";  
    varProcessData = true; 
    CallService(); 
} 

That function then calls this one: 该函数然后调用这个:

//Generic function to call AXMX/WCF  Service         
function CallService()  
{ 
        $.ajax({ 
            type        : varType, //GET or POST or PUT or DELETE verb 
            url         : varUrl, // Location of the service 
                    cache       : false, 
            data        : varData, //Data sent to server 
            contentType : varContentType, // content type sent to server 
            dataType    : varDataType, //Expected data format from server 
            processdata : varProcessData, //True or False 
            success     : function(msg) {//On Successfull service call 
            ServiceSucceeded(msg);                     
            }, 
            error: ServiceFailed// When Service call fails 
        }); 
} 

When I try to run the sample, I get the following in Google Chrome's developer tools, console window: 当我尝试运行该示例时,我在Google Chrome的开发人员工具控制台窗口中获得以下内容:

Failed to load resource: the server responded with a status of 400 (Bad Request) XMLHttpRequest cannot load http://123.123.123.123/NameOfService.svc/GetStuffById . 无法加载资源:服务器响应状态为400(错误请求)XMLHttpRequest无法加载http://123.123.123.123/NameOfService.svc/GetStuffById Origin null is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用null。

The service is working fine and I'm currently calling it from webforms and console apps. 该服务工作正常,我目前正在从webforms和控制台应用程序调用它。 GetStuffById is the method I want to call. GetStuffById是我想要调用的方法。 It accepts a string (GUID in this case) as a parameter and returns a string. 它接受一个字符串(在本例中为GUID)作为参数并返回一个字符串。

The service is a WCF service and is configured to return a SOAP message. 该服务是WCF服务,并配置为返回SOAP消息。 I'd prefer JSON but that's another issue for another question some other day. 我更喜欢JSON,但这是另一个问题的另一个问题。

Any ideas what's going on here? 有什么想法在这里发生了什么? Thanks! 谢谢!

UPDATE #1 - I changed the POST to a GET. 更新#1 - 我将POST更改为GET。 Still no-go. 还是不行。

Looking at your code: 看看你的代码:

  1. All the variables inside BlahBlahBlahWCFXML are not accessible to CallService() . CallService()无法访问BlahBlahBlahWCFXML中的所有变量。
  2. varData 's value has extra ' (single quote). varData的值有额外的' (单引号)。 If the GUID is a constant value you can do away with the string concatenation and simply write: 如果GUID是常量值,您可以取消字符串连接并简单地写:
    varData = '{"stuffId": "12345678-abcd-9012-3456-abcdefghjkl"}';
    However, if GUID is a variable, generate the GUID somewhere else and just put the variable here instead: 但是,如果GUID是变量,则在其他位置生成GUID,并将变量放在此处:
    varData = '{"stuffId": "' + varGUID + '"}';

Looks like you're trying to call a WCF SOAP webservice from jQuery. 看起来你正在尝试从jQuery调用WCF SOAP Web服务。 This isn't going to work out of the box. 这不是开箱即用的。 The WCF SOAP service expects a SOAP message to call it--- simply going to the URL isn't going to work. WCF SOAP服务需要SOAP消息来调用它 - 简单地转到URL是行不通的。 If you insist on WCF there are some extensions for restful WCF which will make this much easier. 如果你坚持WCF,那么宁静的WCF有一些扩展,这将使这更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM