0

我的 jQuery 代码是这样的:

$(document).ready(function () {
  $("input:#baglan").click(function () {
    var Type;
    var Url;
    var Data;
    var ContentType;
    var DataType;
    var ProcessData;
    WCFJSON();

    function WCFJSON() {

      var kullanici = $("input:#ad3").val();
      var sifre = $("input:#sifre").val();
      Type = "POST";
      Url = "http://hacegan:84/SQLbaglantiHACEGAN/Service.svc/GetData";
      Data = '{"value": "' + kullanici + '","sifre": "' + sifre + '"}';
      ContentType = "application/json; charset=utf-8";
      DataType = "json";
      varProcessData = true;
      CallService();
    }

    //function to call WCF  Service      
    function CallService() {
      $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service
        data: Data, //Data sent to server
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server
        processdata: ProcessData, //True or False
        success: function (msg) { //On Successfull service call
          ServiceSucceeded(msg);
        },
        error: ServiceFailed // When Service call fails
      });
    }

    function ServiceFailed(result) {
      alert("basarisiz");
      alert('Service call failed: ' + result.status + '' + result.statusText);
      Type = null;
      varUrl = null;
      Data = null;
      ContentType = null;
      DataType = null;
      ProcessData = null;
    }

    function ServiceSucceeded(result) {
      if (DataType == "json") {
        resultObject = result.GetDataResult;
        alert(resultObject);
      }
    }


  });

我的代码在 Internet Explorer 上运行,但是当我在 Firefox 或 Chrome 上运行此代码时,它会在 ServiceFailed 函数中出现错误。使用此代码,我正在访问 WCF 服务。那么,我怎样才能让它在 Firefox 和 Chrome 中运行呢?

4

3 回答 3

0

在您的代码中,ServiceFailed如果您的 ajax 调用返回错误,则您正在调用一个函数:

error: ServiceFailed

然后,您有两个具有相同名称和签名的函数:

function ServiceFailed(result)

function ServiceFailed(xhr)

使用 Chrome 或 Firebug 中的控制台,您将能够看到您的 ajax 调用。

看看这里:Chrome 的 firebug 跟踪 ajax 请求的技术

一个建议:

而不是手动创建您的 JSON 字符串

Data = '{"value": "' + kullanici + '","sifre": "' + sifre + '"}';

您可以创建对象,然后将其转换为字符串:

JSON.stringify({value: 'foo', sifre: 'bar'})
于 2013-02-11T08:04:00.760 回答
0

我认为这会更好地传递参数:

try putting just click in the doc ready and all the functions outside尝试使用这个。

 $(document).ready(function () {
   $("input:#baglan").click(function () {
    var Type;
    var Url;
    var Data;
    var ContentType;
    var DataType;
    var ProcessData;
    WCFJSON(Type, Url, Data, ContentType, DataType, varProcessData);
   });
 }); // <----------end of doc ready

 function WCFJSON(Type, Url, Data, ContentType, DataType, varProcessData) {
     var kullanici =$("input:#ad3").val();
     var sifre=$("input:#sifre").val();
     Type = "POST";
     Url = "http://hacegan:84/SQLbaglantiHACEGAN/Service.svc/GetData";
     Data = '{"value": "' +kullanici+ '","sifre": "' +sifre+ '"}';
     ContentType = "application/json; charset=utf-8";
     DataType = "json"; 
     varProcessData = true; 
     CallService(Type, Url, Data, ContentType, DataType, varProcessData);
 }

 //function to call WCF  Service      
 function CallService(Type, Url, Data, ContentType, DataType, varProcessData) {
    $.ajax({
    type: Type, //GET or POST or PUT or DELETE verb
    url: Url, // Location of the service
    data: Data, //Data sent to server
    contentType: ContentType, // content type sent to server
    dataType: DataType, //Expected data format from server
    processdata: varProcessData, //<--------------------------should be this
    success: function (msg) { //On Successfull service call
      ServiceSucceeded(msg);
    },
    error: ServiceFailed(err) // When Service call fails
  });
}

function ServiceFailed(result) {
  alert("basarisiz");
  alert('Service call failed: ' + result.status + '' + result.statusText);
  Type = null;
  varUrl = null;
  Data = null;
  ContentType = null;
  DataType = null;
  ProcessData = null;
}

function ServiceSucceeded(result) {
  if (DataType == "json") {
    resultObject = result.GetDataResult;
    alert(resultObject);
  }
}
于 2013-02-11T08:04:27.367 回答
0

它应该是 ProcessData 标志而不是 varProcessData

function WCFJSON() {

  var kullanici = $("input:#ad3").val();
  var sifre = $("input:#sifre").val();
  Type = "POST";
  Url = "http://hacegan:84/SQLbaglantiHACEGAN/Service.svc/GetData";
  Data = '{"value": "' + kullanici + '","sifre": "' + sifre + '"}';
  ContentType = "application/json; charset=utf-8";
  DataType = "json";
ProcessData = true;
  CallService();
}
于 2013-02-11T08:05:39.680 回答