jquery运行php函数,如何使用jQuery的$ .ajax()函数来运行php脚本?

本问题已经有最佳答案,请猛点这里访问。

为了理解这一点,我制作了一个示例代码,因为我的实际代码要大得多。

基本上,我想要完成的是运行我的PHP脚本,它使用Ajax编辑一个XML文件。这是因为我需要在我的实际项目中的一个JavaScript中完成这项工作。

这就是我目前为止所拥有的:

包含ajax函数的.php文件:

function editXMLDoc()

{

$.ajax({

url:"sensors.php",

context: document.body

}).done(function() {

$( this ).addClass("done" );

});

}

Endre XML

下面是写入XML的PHP脚本:

include 'sensor.php';

$b=new sensor();

$arr=$b->load('sensor.xml');

for($i=0,$ms=count($arr);$i

{

if($arr[$i]['fields']['status']=='1')

{

$arr[$i]['fields']['status']='0';

}else{

$arr[$i]['fields']['status']='1';

}

}

echo"Completed
";

//3. save array to xml

$b->save('sensor.xml',$arr);

?>

我知道脚本在工作,所以我非常确定问题在于Ajax函数和PHP脚本之间的连接。

有人能帮我吗?

在Ajax中,您使用函数sensors.php,但php文件称为sensor.php。或者脚本中的文件称为sensors.php?

不,不是。php文件包括另一个php文件sensor.php。我知道这有多令人困惑。

@用户2927356:这是否也回答了您之前的问题stackoverflow.com/q/29278936/367456?

尝试此代码…实际上,您没有附加jquery库。

jQuery(document).ready(function($){

var resp = $("#response");

$.ajax({

type:"POST", // Method type GET/POST

url:"sensors.php", //Ajax Action url

data: {},

// Before call ajax you can do activity like please wait message

beforeSend: function(xhr){

resp.html("Please wait...");

},

//Will call if method not exists or any error inside php file

error: function(qXHR, textStatus, errorThrow){

resp.html("There are an error");

},

success: function(data, textStatus, jqXHR){

resp.html(data);

}

});

});

哈利路亚!谢谢!!

像这样使用Ajax:

jQuery(document).ready(function($){

$('.rt11').click(function(){

$.ajax({

type:"POST", // Method type GET/POST

url:"sensors.php", //Ajax Action url

data: {yourKey:"yourValue", yourKey1:"another value

[collapse title=""]
  • 好的,但是如何用按钮调用这个函数呢?
  • 在Ajax调用中,不需要"data"、"beforesend"和"type"属性来简化它。如果返回XML文件,则添加数据类型:"xml"。
  • 在$('.rt11')内传递元素选择器类或ID。单击(function()。{
  • @很抱歉今天中午没空,但当你说进去的时候,我该怎么做呢?我想应该是这样的:endre-xml
  • 我的意思是,您使用的是class="rt11",而这个类已经在大括号内使用了$(".rt11"),所以单击按钮,Ajax请求将调用。如果您使用的是火狐,那么安装一个名为firebug的插件,然后签入控制台,您将看到Ajax请求。
  • @satesh-hm.如果我放下按钮,让Ajax函数在每次加载页面时都运行,那么函数看起来如何?
  • 然后您可以删除单击函数$('.rt11')。之后,Ajax将在页面加载时自动调用(function())。
  • SATEESH好!因为我怀疑这不起作用,这可能就是为什么我也不能让按钮工作的原因。所以我哪里出错了。为了让Ajax运行它,我需要在PHP脚本中说些什么吗?
  • 不,Ajax是客户端脚本,所以PHP不能禁用它。我想你还有别的问题……你能分享你的链接吗?在那之后,我可以判断是否有错误。
  • 我在本地主机上工作,但如果可以,我可以共享Dropbox链接?
  • 是的,当然…只需分享你的链接
  • 谢谢你,dropbox.com/sh/o07ng25hzc97xvk/aab5k6uqvcxadthrjoyf_4ya?dl=‌&8203;0
[/collapse]
[wp_ad_camp_1]

尝试这个,我认为您错过了包含jquery库文件。在Ajax调用之后,获取发布数据到您的PHP文件。

[cc lang="php"]    

//include the jQuery library file

function editXMLDoc()

{

var myvalue='123';

$.ajax({

url:"sensors.php",

context: document.body,

data: {myvalue:myvalue},

}).done(function() {

$( this ).addClass("done" );

});

}

Endre XML

杰出的!导入是丢失的链接!

HTML:

Endre XML

JS:

$("#idButton").click(function(){

$.ajax({

url: 'sensors.php',

dataType:"xml", //if it returns a xml file

success: function (data) {

// everything is ok

alert(data)

},

error: function (xhr, status, error) {

// Something went wrong

if (xhr.status > 0) alert('Error: ' + status);

}

});

})

我认为代码中没有问题,但有时在大型代码中,min.js文件没有正确包含。请尝试以下代码。不需要更改sensors.php文件。

$(document).ready(function(){

$('.rt11').click(function(){

$.ajax({

url:"sensors.php",

context: document.body

}).done(function(html) {

$( this ).addClass("done" );

});

});

});

Endre XML

试过了。不幸的是,没有运气。sensors.php没有完成设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值