简体   繁体   English

Javascript可在本地运行,但在服务器上运行时会中断

[英]Javascript works locally but breaks when running on server

$(document).ready(function(){
/* The following code is executed once the DOM is loaded */

/* This flag will prevent multiple comment submits: */
var working = false;

/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){

    e.preventDefault();
    if(working) return false;

    working = true;
    $('#submit').val('Working..');
    $('span.error').remove();
    console.log('Test javascript 1')
    /* Sending the form fileds to submit.php: */

    $.post('comments/submit.php',$(this).serialize(),function(msg){
    console.log('Test javascript 2')
        working = false;
        $('#submit').val('Submit');

        if(msg.status){

            /* 
            /   If the insert was successful, add the comment
            /   below the last one on the page with a slideDown effect
            /*/

            $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
            $('#body').val('');
        }
        else {

            /*
            /   If there were errors, loop through the
            /   msg.errors object and display them on the page 
            /*/

            $.each(msg.errors,function(k,v){
                $('label[for='+k+']').append('<span class="error">'+v+'</span>');
            });
        }
    },'json');

});

}); });

This script works fine when I am running it locally however, when I uploaded it to the server it breaks when I click 'submit' and hangs on 'Working..' The problem seems to come from the line 当我在本地运行该脚本时,该脚本运行良好,但是,当我将其上传到服务器时,单击“提交”并挂起“正在运行”时,该脚本会中断。问题似乎出在这行

$.post('comments/submit.php',$(this).serialize(),function(msg){

I cant work out what the problem is. 我无法解决问题所在。 Can anyone help? 有人可以帮忙吗?

Your comments/submit.php parameter is not pointing to the correct location on your server. 您的comments/submit.php参数未指向服务器上的正确位置。 There is going to be some sort of ammending being done to the URL it is running from that is different in your local environment versus the production server. 将对其运行的URL进行某种修改,这与您的本地环境和生产服务器的环境不同。

Take a look at your Network tab in Chrome's Debug menu (or similar in another browser) to see where it's trying to go. 在Chrome的“调试”菜单(或其他浏览器中的类似功能)中查看“网络”标签,以查看尝试去的地方。

Most likely, the solution will be as easy as adding a / to the front of your post path, but there might be another step or two in the URL chain that you're missing. 解决方法很可能就像在帖子路径的前面添加一个/一样简单,但是URL链中可能还缺少另外一两个步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 在本地工作,但上传到服务器时出现错误 - Javascript works locally but bugs when uploaded to server HTML中的Javascript可在本地使用,但不能部署到服务器上 - Javascript in HTML works on locally but not when deployed to a server Javascript 在本地工作,但不在服务器上 - Javascript works locally but not on the server Javascript可在本地使用,但不能在带有niceEdit的服务器上使用 - Javascript works locally but not on server with niceEdit Javascript代码在本地工作,但不能在服务器上工作 - Javascript code works locally, but not on server Bootstrap 的 javascript 在本地工作,但在部署到服务器时不起作用 - Bootstrap's javascript works locally but not when deployed to a server 我的JavaScript在本地运行良好,但是在服务器上时崩溃了,为什么? - My javascript works great locally but crashes when it is on server, why? Javascript在本地运行良好,但不在我的服务器上 - Javascript works great locally, but not on my server 我的JavaScript在本地可以运行,但是不能在服务器上运行吗? - my javascript works locally but not on the server why? golang api请求可在浏览器中使用,但从本地运行的JavaScript请求时返回401(未授权) - golang api request works in browser but return a 401 (Unauthorized) when requested from JavaScript running locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM