ajax怎么发送数据给php,使用AJAX将数据发送到PHP (Sending data to PHP using AJAX)

2015-10-21 04:52:45

0

I have searched my problem before posting this question, but failed to find a solution.

I need to send a json string to php file but unable to do so, can some one please help with my problems below: I'm new to php and jquery and is struggling, Need your cooperation please.

I have a function that captures data on the text file:

function updateVal() {

var node_list = document.getElementsByTagName('input');

var c = 0;

var fieldName = [];

var fieldText = []

var ID = [];

for (var i = 0; i < node_list.length; i++) {

var node = node_list[i];

if (node.getAttribute('type') == 'text') {

fieldName[c] = node.name;

fieldText[c] = node.value;

ID[c] = node.id;

c++;

}

}

var postData = {

fieldName: fieldName,

fieldText: fieldText,

ID: ID

};

var dataString = JSON.stringify(postData);

console.log(JSON.stringify(postData));

$.ajax({

type: "POST",

dataType: "json",

url: "update.php",

data: {myData:postData}

})

//return JSON.stringify(postData);

}

My update.php is like this:

$json = $_POST['json'];

$result = json_decode($json);

echo $result;

echo $_POST['myData']);?>

On loading this: I'm getting the following error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Moreover, I'm not sure if the data being sent to php or not.

Can experts pls validate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ESP8266向php发送数据的示例代码: ```c++ #include <ESP8266WiFi.h> #include <WiFiClient.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* host = "your_PHP_host"; void setup() { Serial.begin(115200); delay(10); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); } void loop() { WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("Connection failed"); return; } // We now create a URI for the request String url = "/your_php_script.php"; url += "?data="; url += "your_data_here"; Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n" + "\r\n"); delay(500); // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); delay(10000); // Wait for 10 seconds before sending the next request } ``` 在这个示例中,你需要将以下变量替换为你的实际值: - ssid: 你的WiFi网络名称 - password: 你的WiFi网络密码 - host: 你的PHP主机的地址 - url: 你的PHP脚本的路径和文件名 - data: 你要发送PHP脚本的数据 此代码将通过GET请求发送数据PHP脚本,并在串口监视器中打印服务器的响应。你可以根据需要更改请求类型和数据格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值