2017-09-12 44 views
-1

我試圖讓一個簡單的Ajax後的工作。當我使用Fiddler將標題和數據發佈到URL時,它會返回Ok。然而,從我的ajax代碼,它轉到錯誤:函數。阿賈克斯後錯誤 - 你可以看到代碼錯誤?

$(document).ready(function() { 
     $('#lblAjaxStatus').html('ready'); 

     $('#btnAjaxLogin').click(function() { 
      $.ajax(
      { 
       url: "https://www.testurl.com/api/login", 
       type: "POST", 
       contentType: "application/json", 
       headers: { 
        "From":"CC", 
        "To":"CCM", 
        "Language":"0", 
        "Offset":"-8" 
       }, 
       data: { "loginId": "[email protected]", "password": "Password1", "vehicleType": "E", "deviceId": "ABCDEF", "clientId": "123456", "appVersion": "", "osType": "", "osVersion": "" }, 
       success: function (data) { 
        // results 
        $('#lblAjaxStatus').html('here '); 
       }, 
       error: function (error) { 
        $('#lblAjaxStatus').html('error ' + error.getResponseHeader + '\n' + error.responseText); 
       } 
      }); 
     }) 

我試圖設置透過JSON.stringify(ETC),從各種臺式機測試數據,甚至改變了URL搬到別的地方去......結果總是跳轉到錯誤的功能和顯示功能 (一) {風險b;若(K){如果(H!){H = {};而(b = Eb.exec(G))H [b [1] .toLowerCase()] = b [2]} b = h [a.toLowerCase()]}} return null == b?null:b} undefined

我不知道我的結構或語法有什麼問題?任何人都以我的方式看到'錯誤'?謝謝。

+0

你有沒有試圖消除的contentType選項,或將其更改爲「應用/ JSON的; charset = UTF-8'? –

+0

如果你打算髮佈一個JSON負載,你應該在'data'上使用'JSON.stringify'。此外,'getResponseHeader'是一個函數,所以應該是'error.getResponseHeader()'。我建議你將你的錯誤處理程序改爲'error:function(jqXhr,textStatus,errorThrown){$('#lblAjaxStatus')。html('error'+ textStatus +'\ n'+ errorThrown)}'。 – Phil

回答

0

與這一個

<script> 
    $(document).ready(function() { 

     $('#lblAjaxStatus').html('ready'); 

     $('#btnAjaxLogin').click(function() { 

      $.ajax({ 
       url: "your-ajax-file.php", 
       type: "POST", 
       contentType: "application/json", 
       headers: { 
        "From":"CC", 
        "To":"CCM", 
        "Language":"0", 
        "Offset":"-8" 
       }, 
       data: { loginId: "[email protected]", password: "Password1", vehicleType: "E", deviceId: "ABCDEF", clientId: "123456", appVersion: "", osType: "", osVersion: "" }, 
       success: function (data) { 
        // results 
        $('#lblAjaxStatus').html(data); 
       }, 
       error: function (error) { 
        $('#lblAjaxStatus').html('error ' + error.getResponseHeader + '\n' + error.responseText); 
       } 
      }); 
     }); 
    }); 

</script> 

您的Ajax-file.php

<?php 
    echo "Put The HTML Data here"; 
?> 

我希望這可以幫助你更換你的JavaScript腳本代碼。

0

你不需要的時候你傳遞參數添加雙引號。刪除參數中的所有雙引號。

data: $("#form").serialize() // #form is id of your form 

您可以使用input名獲取所有變量的值

+0

看來OP要張貼JSON有效載荷。這將把數據格式化爲'application/x-www-form-urlencoded' – Phil