簡體   English   中英

jQuery Ajax不向php服務器發送數據

[英]Jquery Ajax not sending data to php server

這對您來說很容易,但是經過多年的在線查找后,我找不到解決方案,或者至少我知道一種解決方案。

結果用戶變量都可以正確發出警報。 數據內容顯示為空-為什么?

我應該如何將這兩個變量插入數據?

(同樣如果正確的話-我如何在不進入服務器端的情況下查看數據內容)

先感謝您。

        var result = result.text;
        var users = localStorage.getItem('user');
         alert("1");
               $.ajax({
   url:'********',
   method: 'POST',
   data: {user: users, serial: result},
   dataType: "text",
   contentType: "application/json",
   success: function(data){


       alert (data);
       alert(users);
       alert(result);

在代碼中, alert(data)將引用服務器返回的data 不是您的ajax請求的data

$.ajax()settings對象作為其參數。 這是一組配置Ajax請求的鍵/值對。 data只是傳遞給$.ajax()方法的對象的鍵之一。

為了簡化您的代碼,這是正在發生的事情:

// this is what you're sending to the server
var dataSentToTheServer = {
    user: users,
    serial: result 
}

// creating an object where data, url, success etc are keys.
var settings = {
    url:'api/method',
    method: 'POST',
    data: dataSentToTheServer,
    dataType: "text",
    success: function (dataSentFromTheServer) {
        alert(dataSentFromTheServer); // this argument will have the data returned by your server
        alert(dataSentToTheServer); // data which was sent *to* the server
        alert(data); // doesn't exist
    }
    ....
}

$.ajax(settings);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM