尝试使用jQuery Ajax调用PHP文件

尝试使用jQuery Ajax调用PHP文件

问题描述:

我正在Debian 7.0.0上使用jQuery 2.0.2进行开发.

I am developing using jQuery 2.0.2 on Debian 7.0.0.

我正在尝试使用jQuery.ajax从JavaScript文件调用PHP脚本.我的代码如下.

I am trying to call a PHP script from a JavaScript file using jQuery.ajax. My code is as follows.

    jQuery.ajax({
    type: "POST",
    url: "DisplayParameters.php",
    data: { LUT:lut, 
        Brightness: brightness,
        Gamma: gamma,
        Background: background}
    }).done(function( result ) {
    $("#msg").html( " Ajax called" );
    });  

在DisplayParameters.php的开头,我有以下内容.

At the beginning of DisplayParameters.php, I have the following.

?>
<script language="JavaScript">
alert('DisplayParameters');
</script>
<?php

,但未调用警报框. Firebug继续进行,没有任何错误,并显示DisplayParameters.php文本(带有+/-控件以将其折叠和展开).但是不会调用警报框.我无法从DisplayParameters.php获得任何反馈来跟踪正在发生的事情.

but the alert box is not called. Firebug proceeds without any errors and shows the DisplayParameters.php text (with a +/- control to collapse and expand it). But the alert box is not called. I cannot get any feedback from DisplayParameters.php to trace what is happening.

对我来说,从DisplayParameters.php获取反馈(例如警报框或console.log)的最佳方法是什么?

What would be the best way for me to get feedback (like alert boxes or console.log) from DisplayParameters.php?

您的ajax调用以文本形式返回DisplayParameters.php的内容,console.log(result)将显示:

Your ajax call returns the contents of DisplayParameters.php as text, console.log(result) would show:

<script language="JavaScript">
alert('DisplayParameters');
</script>

如果您想将此额外的代码添加到页面中(除非您确定不会破坏当前页面,否则我不建议这样做),您可以尝试:

If you want to add this extra code to the page (which I don't recommend unless you are sure you won't break the current page), you can try:

jQuery.ajax({
    type: "POST",
    url: "DisplayParameters.php",
    data: { LUT:lut, 
        Brightness: brightness,
        Gamma: gamma,
        Background: background}
}).done(function( result ){
    $("#msg").html( " Ajax called" );
    $("body").append(result);
});

另一种方法是序列化(JSON)所需的信息,设置dataType: "json"选项,然后根据此信息进行操作.

Another approach would be to serialize (JSON) the information you need, set the dataType: "json" option, and act based on this information.

如果您要发送HTML,请确保您的PHP代码没有显示任何错误或警告消息.

If you will send HTML, ensure your PHP code doesn't shows any error or warning messages.