为什么我不能访问我的JavaScript WCF Web服务? [英] Why can't I access my WCF web service with Javascript?

查看:149
本文介绍了为什么我不能访问我的JavaScript WCF Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的AJAX和我试图访问一个WCF Web服务的方式如下:

I'm new at AJAX and I'm trying to access a WCF web service the following way:

$(function () {
    $('#formNew').submit(function () {
        var datos = {
            "Nombre": $('#nombre').val(),
            "ApellidoP": $('#appP').val(),
            "ApellidoM": $('#appM').val(),
            "UserName": $('#username').val(),
            "Password": $('#password').val(),
        };
        var args = "Data=" + JSON.stringify(datos);
        var url = 'http://127.0.0.1:81/SismosService.svc/usuario/new?' + args;
        alert(url);
        $.ajax({
            type: 'GET',
            url: url,
            success: function (data) {
                alert("Exito " + JSON.stringify(data));
            },
            error: function (data) {
                alert("Error " + JSON.stringify(data));
            }
        });
    });
});

当我填写表格,然后点击提交按钮,我得到的萤火虫出现以下错误:

When I fill the form and click the submit button, I get the following error on Firebug:

N

S_ERROR_NOT_AVAILABLE: prompt aborted by user
[Break On This Error]   

throw Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE)

我的Web服务的定义如下:

My web service is defined as follows:

    [WebGet(UriTemplate = "/usuario/new?Data={data}",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare)]
    ResponseObject<Usuarios> NewUsuario(string data);

我是什么做错了吗?是不是我送的参数的方法是什么?难道就是这样,我想访问Web服务?感谢您的帮助。

What am I doing wrong? Is it the way I'm sending the parameters? Is is the way I'm trying to access the web service? Thanks for any help.

推荐答案

我通常使用下面的配置,使AJAX调用我的WCF服务:

I normally use the following configuration to enable ajax calls to my WCF services:

1)首先,我创建在Web.config中一个JSON端点的行为和我的服务将其联系起来:

1) First I create a JSON endpoint behaviour in Web.config and associate my service to it:

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="WebHttpJson">
        <webHttp defaultBodyStyle="Wrapped"
                 defaultOutgoingResponseFormat="Json" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name="MyApp.LoginService">
      <endpoint address=""
                behaviorConfiguration="WebHttpJson"
                binding="webHttpBinding"
                contract="MyApp.LoginService" />
    </service>
  </services>
</system.serviceModel>

2)然后,我可以简单地这样定义我的WCF服务:

2) Then I can simply define my WCF service like this:

[ServiceContract]
public class LoginService
{
    [OperationContract]
    public void SignIn(string email, string pswd)
    {
        // Check credentials and create session cookie
    }
}

3)最后做的jQuery的Ajax调用一样,显示如下:

3) And finally make jQuery ajax calls like showed below:

$.ajax({
    contentType: 'application/json; charset=utf-8',
    url: serviceUrl + '/SignIn',
    type: 'POST',
    data: JSON.stringify({
        email: 'john.doe@abc.com', 
        pswd: 'qwerty'
    }),
    success: function () { alert('success!'); },
    error: function () { alert('error!'); }
});

这篇关于为什么我不能访问我的JavaScript WCF Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆