php $ 未在 javascript 中定义 ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19578121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:44:05  来源:igfitidea点击:

$ not defined ajax request in javascript

javascriptphpjqueryajaxvariables

提问by user2793027

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?

这是分配它们的正确方法吗?

回答by Adam Rackis

The srcon 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>

回答by hexacyanide

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.

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