我的AJAX调用仅适用于Internet Explorer

时间:2013-02-11 07:52:34

标签: jquery ajax internet-explorer

我的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);
      }
    }


  });

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

3 个答案:

答案 0 :(得分:0)

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

error: ServiceFailed

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

function ServiceFailed(result)

function ServiceFailed(xhr)

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

看看这里:Chrome's firebug's technic to track ajax requests

建议:

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

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

您可以创建Object,然后将其转换为String:

JSON.stringify({value: 'foo', sifre: 'bar'})

答案 1 :(得分: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);
  }
}

答案 2 :(得分: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();
}