Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在带有反馈的外部服务器上运行PHP脚本(通过AJAX)只返回错误405_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 在带有反馈的外部服务器上运行PHP脚本(通过AJAX)只返回错误405

Javascript 在带有反馈的外部服务器上运行PHP脚本(通过AJAX)只返回错误405,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我的问题是:我有一个用于发送电子邮件的HTML表单。我有一个PHP文件来处理上述表格并发送电子邮件。问题是,它们不在同一台服务器上(不过我可以访问PHP文件)。我的页面上有引导和JQuery 我的表格如下:(简化) 几个文本字段,每个字段都有名称和id 提交 这是一个简单的形式,它的工作。问题是如何将数据发送到PHP脚本。这些脚本标记位于标记的末尾: <script> $(document).ready(function() { $('#ajax-contact').on(

我的问题是:我有一个用于发送电子邮件的HTML表单。我有一个PHP文件来处理上述表格并发送电子邮件。问题是,它们不在同一台服务器上(不过我可以访问PHP文件)。我的页面上有引导和JQuery

我的表格如下:(简化)


几个文本字段,每个字段都有名称和id
提交
这是一个简单的形式,它的工作。问题是如何将数据发送到PHP脚本。这些脚本标记位于
标记的末尾:

<script>
$(document).ready(function() {  

$('#ajax-contact').on('click', function () {
              // Get the form.
              var form = $("#ajax-contact");

              // Get the messages div.
              var formMessages = $("#form-messages");

              // Set up an event listener for the contact form.
              $(form).submit(function(event) {
                // Stop the browser from submitting the form.
                event.preventDefault();

                // Serialize the form data
              var formData = $(form).serialize();
                });

              $.getJSON("my_path_to_the_php_script.php", function(formData){

              });
});
});

</script>

$(文档).ready(函数(){
$('ajax contact')。在('click',函数(){
//去拿表格。
var form=$(“#ajax联系人”);
//获取消息div。
var formMessages=$(“#表单消息”);
//为联系人窗体设置事件侦听器。
$(表格)。提交(功能(事件){
//停止浏览器提交表单。
event.preventDefault();
//序列化表单数据
var formData=$(form).serialize();
});
$.getJSON(“my_path_to_the_php_script.php”),函数(formData){
});
});
});
我被告知使用getJSON来解决PHP脚本与HTML不在同一服务器上这一事实。getJSON向PHP文件发送一个JSONP字符串。我要检查的只是PHP脚本是否失败,并在表单上方显示一条错误消息

我尝试使用.getJSON的原因是因为我已经尝试了以下方法:

 $.ajax({
                url:'my_path_to_the_php_script.php',
                type:'post',
                data: formData,
                jsonp: 'jsonp',
                dataType: 'jsonp', // use JSONP
                success: function(data){
                      // Display OK message
                      $(formMessages).html(<div class="alert alert-success">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">X</a>
                        Success!
                      </div>)

                      // Clear the form.
                      $(formMessages).each(function(){
                      this.reset();
                      });
                    }
                },
                error: function (error) {
                $(formMessages).html(<div class="alert alert-danger">
                  <a href="#" class="close" data-dismiss="alert" aria-label="close">X</a>
                  Warning Message!
                </div>)
                }

    });
$.ajax({
url:'my_path_to_the_php_script.php',
类型:'post',
数据:formData,
jsonp:'jsonp',
数据类型:'jsonp',//使用jsonp
成功:功能(数据){
//显示OK消息
$(formMessages).html(
成功!
)
//清除表格。
$(formMessages)。每个(函数(){
这是reset();
});
}
},
错误:函数(错误){
$(formMessages).html(
警告信息!
)
}
});
两种方法都不起作用。我只想检查脚本是否失败。$.getJSON()方法不起作用,它保持在同一页上(正如我所期望的),但服务器返回一个错误405页(不允许使用该方法)。我没有访问该服务器PHP安装的头文件的权限。$.ajax(数据类型:“jsonp”)方式不起作用,返回错误405


感谢阅读,请提供帮助。

如果
form
已经存储了jQuery对象(例如
$(“#ajax contact”)
,则不需要
$(form)
,只需使用
form.submit()。此外,如果变量名存储jQuery对象(即
$form
),请养成在变量名前面加上
$
的习惯。如果变量名位于不同的服务器上,则需要在ajax函数的
url
值中包含完整的url。我会重写这个。表单提交处理程序不应该在click Handlerso中,
jsonp
不支持
POST
,这可能就是为什么您不需要
$(表单)
如果
form
已经存储了jQuery对象(例如
$('.\ajax contact')
),只需使用
form.submit()。此外,如果变量名存储jQuery对象(即
$form
),请养成在变量名前面加上
$
的习惯。如果变量名位于不同的服务器上,则需要在ajax函数的
url
值中包含完整的url。我将重写此内容。表单提交处理程序不应位于单击句柄中,
jsonp
不支持
POST
,这可能是您获得405的原因
 $.ajax({
                url:'my_path_to_the_php_script.php',
                type:'post',
                data: formData,
                jsonp: 'jsonp',
                dataType: 'jsonp', // use JSONP
                success: function(data){
                      // Display OK message
                      $(formMessages).html(<div class="alert alert-success">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">X</a>
                        Success!
                      </div>)

                      // Clear the form.
                      $(formMessages).each(function(){
                      this.reset();
                      });
                    }
                },
                error: function (error) {
                $(formMessages).html(<div class="alert alert-danger">
                  <a href="#" class="close" data-dismiss="alert" aria-label="close">X</a>
                  Warning Message!
                </div>)
                }

    });