$未在javascript中定义ajax请求

时间:2021-09-10 08:11:37

I am trying to send a php file some values using ajax but in the call for ajax I am getting the following error

我试图使用ajax发送一个PHP文件一些值,但在调用ajax我收到以下错误

Uncaught ReferenceError: $ is not defined 

at the beginning line for the ajax request as follows:

在ajax请求的开头行如下:

$.ajax({
  type: "POST",
  url: 'program3.php',
  data: {
    player1name: player1name.value,
    player2name: player2name.value,
    playtopoints: playtopoints.value,
    delay: delay.value,
    numgames: numgames.value,
    gamesplayed: gamesplayed.value,
    p1turn: p1turn.value,
    p2turn: p2turn.value,
    p1total: p1total.value,
    p2total: p2total.value
  },
  success: function (data) {
    rolling = data;
  }
});            

I first thought that it might need the refrence to ajax so i added the following line before the javascript on the html page:

我首先想到它可能需要引用ajax,所以我在html页面上的javascript之前添加了以下行:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

but i am still getting the erro can anyone offer any insight?

但我仍然得到错误任何人都可以提供任何见解?

Also i have the data variables all defined as follow:

我也将数据变量定义如下:

var player1name = document.JForm.p1name.innerHTML;

is that the correct way to assign them?

分配它们的正确方法是什么?

2 个解决方案

#1


6  

The src on your script tag is invalid—at least if you're not running this from http or https. Replace

脚本标记上的src无效 - 至少如果您没有从http或https运行它。更换

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

with

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

#2


3  

You are probably accessing the file locally, which won't work with a protocol-relative script tag.

您可能在本地访问该文件,这不适用于协议相关的脚本标记。

<!-- access from http resolves to this -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- local access resolves to this -->
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

The file wouldn't have existed locally, and the script would've never been loaded. Therefore, the variable $ would then be undefined.

该文件本地不存在,并且该脚本永远不会被加载。因此,变量$将是未定义的。

#1


6  

The src on your script tag is invalid—at least if you're not running this from http or https. Replace

脚本标记上的src无效 - 至少如果您没有从http或https运行它。更换

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

with

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

#2


3  

You are probably accessing the file locally, which won't work with a protocol-relative script tag.

您可能在本地访问该文件,这不适用于协议相关的脚本标记。

<!-- access from http resolves to this -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- local access resolves to this -->
<script src="file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

The file wouldn't have existed locally, and the script would've never been loaded. Therefore, the variable $ would then be undefined.

该文件本地不存在,并且该脚本永远不会被加载。因此,变量$将是未定义的。