如何使用JQuery Ajax将数据发送到PHP文件? [英] How to send data to PHP file using JQuery Ajax?

查看:111
本文介绍了如何使用JQuery Ajax将数据发送到PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery和Ajax将数据发送到php文件.但是,我只能以json格式接收来自ajax的响应,并且无法发送数据.

I am trying to send data to php file using jQuery and ajax. However, I am only able to receive the response from ajax in json format and unable to send data.

    $.ajax({
        url: 'myFile.php',
        type: 'GET',
        data: {ID:1}, 
        dataType:'json',
        cache: false,
        beforeSend:function(e){},
        processData: '',
        success: function(response){
            Initialize(response);   
        },
        error: function(err){alert('error')}
    });

推荐答案

您已经做好了一切.

$.ajax({
  url: 'myFile.php',
  type: 'GET',
  data: {
    ID: 1,
    Test: 'Anothervalue'
  },
  dataType: 'json',
  cache: false,
  beforeSend: function(e) {},
  processData: '',
  success: function(response) {
    Initialize(response);
  },
  error: function(err) {
    alert('error')
  }
});

使用php可以通过

$_GET['ID'] $_GET['Test']

您也可以通过使用url来发送数据

You can send data by concating with url also

myFile.php?ID=1&Test=Anothervalue

但是如果要使用POST发送数据或要发送大量数据,则始终可以选择data: {}格式进行发送,因为它更干净.

But if want send data with POST or have much data to send you can always choose data: {} format to send because it's cleaner.

data: {
    ID: 1,
    Test: 'Anothervalue',
    // More
}

这篇关于如何使用JQuery Ajax将数据发送到PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆