[英]include javascript file in php
我有一个 html 文件(index.html),它通过 javascipt 调用 php 文件(pdj.php),并在 a 中填充 output。 这行得通。
$.ajax({
url: '../command/pdj.php',
type: "POST",
data: ({xparam: xparam}),
success: function(data){
var arraydata = $.parseJSON(data);
$("#pdj").html(arraydata[0]);
}
});
php 文件需要 javascript 文件 (spinner.js) 才能工作。
1)在index.html中添加javascipt文件不使用。
2) 包含在 pdj.php 中会导致错误 include_once('spinner.js');
Uncaught SyntaxError: Unexpected token / in JSON at position 1
at Function.parse [as parseJSON] (<anonymous>)
at Object.success (pdj.js:40)
at fire (jquery-3.3.1.js:3268)
我猜我的 ajax 调用是错误的......你能建议如何解决这个问题吗?
提前致谢
您的 ajax 调用没有错。
1)在index.html中添加javascipt文件不使用。
这里的问题是,当 javascript 文件执行时,它找不到目标,因为 ajax 调用尚未执行,这意味着尚未为微调器创建 DOM spinner.js
。
2) 包含在 pdj.php 中会导致错误 include_once('spinner.js');
Here include_once will just add the javascript file's content to the response and this results the broken json and that's why the ajax fails because the content-type is set to application/json
in you ajax config
解决方案是在 ajax 成功处理程序上调用spinner.js
的 function 。
如果您可以提供 spinner.js 作为链接,那将是更好的答案。
$.ajax({
url: '../command/pdj.php',
type: "POST",
data: ({xparam: xparam}),
success: function(data){
var arraydata = $.parseJSON(data);
$("#pdj").html(arraydata[0]);
$("input[type='number']").inputSpinner();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.