2017-05-26 55 views
0

我需要將json文件傳遞給API,但它看起來像我需要在JSON文件發送到API之前做一些事情。錯誤是「無法解碼JSON數據」。何處添加內容類型 - 應用程序/ JSON格式爲html?錯誤 - 無法解碼JSON數據

<form name="myform" enctype="multipart/form-data" action="https://api-106.dxi.eu/ecnow.php" method="POST" enctype='application/json'> 
    <input type="hidden" name="method" value="ecnow_records"> 
    <input type="hidden" name="token" value="xxxxxxxxxxxxxxxxxxxxx"> 
    <input type="hidden" name="action" value="create"> 
    <input type="hidden" name="format" value="json"> 
    <input type="hidden" name="raw" value="1"> 

    Send this file: <input name="easycall" type="file"> 
    <input type="submit" value="Send json File"> 
</form> 

它工作正常,當我嘗試郵遞員並將JSON粘貼到Body-> Raw中。我不確定Body-> Raw在HTML表單中的確切位置?

請指教。

在此先感謝

+0

您在窗體中使用了'enctype =「multipart/form-data」'。順便說一句,你不能在這裏使用原始的JSON體,因爲你有輸入字段文件。 – Suresh

+0

謝謝。那麼我可以使用什麼樣的enctype。 –

+0

截至目前,瀏覽器將不支持'enctype ='application/json''。默認值將是URL表單編碼。對於ref,https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctype。請使用ajax請求發送json作爲原始有效載荷。 – Suresh

回答

0

最後,我得到它的工作方式。感謝您的幫助。

$(document).ready(function(){ 

     $.getJSON('dataset.json', function (data) { 

      $.ajax({      
        url: "https://xxxxxxxxxxxx", 
        xhrFields: 'withCredentials:true', 
        type: "POST", 
        data: JSON.stringify(data), 
        contentType: 'application/x-www-form-urlencoded', 
        success: function (data) { 
         alert("success"+data); 
        }, 
        error: function (xhRequest, ErrorText, thrownError) { 
         alert("Failed to process correctly, please try again"); 
        } 
       }); 



     }); 
相關問題