2013-02-11 57 views
0

我jQuery代码是这样的:我的AJAX调用仅在Internet Explorer工作

$(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中使用它?

+0

这是一个写作excaption :-)现在我删除了这个功能,但我的代码是不是在Firefox上运行或铬 – 2013-02-11 07:58:52

+0

'processdata:ProcessData,// True or False'从哪里得到该布尔值。我认为这应该是'processdata:varProcessData,' – Jai 2013-02-11 08:00:27

+0

'input:#baglan'?只需带上ID#'baglan'即可。 (同上)。它看起来像jQuery会忽略':';本身作为一个CSS选择器它是无效的,但jQuery似乎只是忽略它。 – 2013-02-11 08:01:25

回答

0

在代码中,你调用一个函数ServiceFailed如果你的Ajax调用返回一个错误:

error: ServiceFailed 

你有那么两个函数具有相同的名称和签名:

function ServiceFailed(result) 

function ServiceFailed(xhr) 

在Chrome或Firebug中使用控制台,您将能够看到哟你的阿贾克斯呼叫。

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

一个建议:

不用手动创建JSON字符串

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

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

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

如何在我的代码中使用该行 – 2013-02-11 08:15:55

+0

JSON.stringify返回一个字符串。你可以做Data = JSON.stringify(...),顺便说一句。你的代码中有很多错误,这可能不是最重要的,请尝试阅读一本关于Javascript – JohnJohnGa 2013-02-11 08:22:18

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); 
    } 
} 
+0

的好书。谢谢。但我的问题是运行 – 2013-02-11 08:10:44

+0

你可以试试这种方式。 – Jai 2013-02-11 08:18:01

+0

Nothing :-( – 2013-02-11 08:29:46

0

它应该是过程数据标志,而不是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(); 
} 
相关问题