简体   繁体   English

如何在Ajax请求上发送JavaScript?

[英]How do I send javascript on ajax request?

This is my first time messing with ajax with javascript and I would hardly describe myself as a web developer or web programmer. 这是我第一次用JavaScript搞砸ajax,我几乎不会形容自己是一名Web开发人员或Web程序员。 So I'm going to give you what I have coded and have tried first and then describe my problem and what it's suppose to do last. 因此,我将向您介绍我先编写并尝试过的内容,然后描述我的问题以及最后要做的事情。

So I have a select div with some stuff in it. 所以我有一个选择div,里面有一些东西。

<select id="month" onchange="refreshGraph()">

And when an option is chosen this javascript is suppose to work. 并且当选择一个选项时,此javascript应该可以工作。

function refreshGraph()
{
            var index = document.getElementById('month').selectedIndex;
            var monthOptions = document.getElementById('month').options;
            var selectedDays = monthOptions[index].value;

            $.ajax({
                    url:"divisions.php",
                    type: "POST",
                    dataType: "script",
                    data: {days:selectedDays},
                    success: function(data){
                            $('#divisions').html(data);}
                    });
}

I've also tried rewriting the success function call to be like this: 我还尝试过重写成功函数调用,如下所示:

$('#divisions').html('<script src="http://code.highcharts.com/highcharts.js">
    <script src="http://code.highcharts.com/modules/exporting.js">'+data+'<\/script<\/script>')

Instead of the $.ajax() I've also tried this 除了$ .ajax()我还尝试了

$('#divisions').load("divisions", {days: selectedDays});

This is suppose to update a table with a div with id of divisions. 假设用div的ID为Divid的表更新表。

<table>
  <tr>
    <td>
      <div id="divisions" style="min-width: 90%; height: 50%; margin: 0 auto"></div>
    </td>
  </tr>
</table>

Now divisions.php is a file that has some database calls and a javascript highchart written in plain text meaning there's no php or script or anything else like that surrounding the plain text javascript code that is the highchart. 现在,divisions.php是一个文件,具有一些数据库调用和以纯文本格式编写的javascript highchart,这意味着没有php或脚本或诸如此类的围绕highchart的纯文本javascript代码的文件。 So after all my php it begins like this 所以毕竟我的PHP就这样开始

$(document).ready(function () {
    $('#divisions').highcharts({
        chart: {

Here are my source files I'm including in the header of index_new.php: 这是我包含在index_new.php标头中的源文件:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
    <script src="divisions.php"></script>

So what am I trying to do? 那我想做什么? I'm trying to update the chart given the choice someone chooses in the dropdown. 考虑到有人在下拉菜单中选择的选项,我正在尝试更新图表。 The values in the dropdown are additional sql parameters that get passed to divisions.php . 下拉列表中的值是传递给divisions.php其他sql参数。 divisions.php then uses this additional sql parameter in its calling which affects what the table looks like. 然后divisions.php在其调用中使用此附加sql参数,这会影响表的外观。

On first loading of index_new.php highchart gets loaded and everything is fine. 首次加载index_new.php highchart被加载,一切正常。 The problem I'm facing is when I choose any of the options in the dropdown the chart updates BUT instead of showing a highchart it shows the plain text javascript (see below) that was previously interpreted as actual javascript when index_new.php was first called. 我面临的问题是,当我在下拉列表中选择任何选项时,图表会更新BUT,而不是显示高图,而是显示纯文本javascript(请参见下文),该文本先前在首次调用index_new.php时被解释为实际javascript。 。 So the ajax call is working and I'm grabbing the variable correctly but I'm guessing it's the javascript I have in my header that's not styling it? 因此,ajax调用正在工作,并且我正确地获取了变量,但是我猜这是我的标头中包含的javascript样式不正确吗? I have no clue why this isn't working. 我不知道为什么这不起作用。

Instead of a highchart I get this when updated (Yes I did replace the actual names with name1, name2,...etc): 更新时,我得到的不是高位图(是的,我确实用name1,name2等替换了实际名称):

$(document).ready(function () { $('#divisions').highcharts({ chart: { type: 'column' }, title: { text: 'Total Sales By Salesperson' }, xAxis: { title: { text: 'Salesperson'}, categories: ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', ] }, yAxis: { min: 0, title: { text: 'Total Sales' } }, tooltip: { formatter: function() { return ''+ this.x +'
'+ this.series.name +': '+ this.y +'
'+ 'Total: '+ this.point.stackTotal; } }, legend: { backgroundColor: '#FFFFFF', reversed: true }, plotOptions: { }, series:[ {name: 'Business Equipment' , data: [0, 400, 80000, 68496, 15800, 170000, 155200, ]},{name: 'Corporate Printing' , data: [0, 0, 0, 6600, 0, 0, 0, ]},{name: 'Document Storage' , data: [0, 0, 0, 0, 0, 18000, 0, ]},{name: 'IT Services' , data: [10000, 0, 0, 4500, 0, 0, 2000, ]},]});})

Why don't you use the ajax method of loading data from PHP script into the highchart? 为什么不使用ajax方法将PHP脚本中的数据加载到highchart中?

$(function () {

    $('#month').change(function()
    {
        $.get('divisions.php?selectedData='+ $(this).val(), function (divisions_data)
        {            
            $('#container').highcharts({
                data: {
                    csv: divisions_data
                },
                title: {
                    text: 'My chart title'
                },

                ... rest of your init code ...
            });
        });
    });

});

Then on your divisions.php will render the data required for your chart. 然后在您的divisions.php中将呈现图表所需的数据。 For data structure, read this: http://api.highcharts.com/highcharts#series.data 有关数据结构,请阅读以下内容: http : //api.highcharts.com/highcharts#series.data

More info: http://www.highcharts.com/demo/line-ajax 更多信息: http : //www.highcharts.com/demo/line-ajax

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM