ajax 提示 type is not defind !

KB931125 2017-11-16 05:17:38


如上图,ajax 提示 type is not defind !,data is not defind,url is not defind,dataType is no defind
<script type="text/javascript">
// var map = new BMap.Map("allmap");
// map.centerAndZoom("广州市", 15);
$(document).ready(function(){
$.ajax({
type: "POST",
cache: false,
data: "",
url: "/DeviceMonitor/GetPersonInfo",
dataType: "json",
success: function (data) {
var stations=JSON.stringify(data);
alert(stations);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//这个error函数调试时非常有用,如果解析不正确,将会弹出错误框
alert(XMLHttpRequest.responseText);
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus); // parser error;
}

});
// alert("跳到这啦");
})
</script>
...全文
337 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2017-11-16
  • 打赏
  • 举报
回复
type、url、datatype不是变量,并没有值,他们只是json对象的key
KB931125 2017-11-16
  • 打赏
  • 举报
回复
不是断点的问题啊,我是说那些个type、url、datatype、之类的和里面的变量stations都提示is not defind呀,数据我可以取出打印出来啊
Hello World, 2017-11-16
  • 打赏
  • 举报
回复
断点在success里面,超出了作用域,那些变量都是不可访问的
In order to AJAX enable it we will have to add the following attribute to the Web Service declaration part:
[System.Web.Script.Services.ScriptService()]
When this is done our Web Service is ready to respond to client-side JavaScript calls.
One more thing has to be done here: we need the Web Method that we will call from client-side JavaScript. Let us define it like this:
[WebMethod]
public string GetServerResponse(string callerName)
{
if(callerName== string.Empty)
throw new Exception("Web Service Exception: invalid argument");

return string.Format("Service responded to {0} at {1}", callerName, DateTime.Now.ToString());
}
Configuring ASP.Net Application
ASP.Net applications web.config file also has to be modified in order to enable Web Service calls from client-side JavaScript code.
This modification is though made for you by Microsoft Visual Studio 2005 if you are using ASP.Net AJAX template. Here is the example of what can be inserted into httpHandles section of your web.config file:

...

...


type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
...

...

...

Making client-side JavaScript code
Let us take a look at the default.aspx file that was automatically created in our project (if it was not - then you will have to add one manually). First we have to make sure that we have one and only one instance of Script Manager object on your page:







Then we have to add Services collection to our ScriptManager object, add ServiceReference to the Services collection and specify Path to the desired service.
The result might look like this:










or like this if you prefer to do it directly in your code-behind file :
ScriptManager1.Services.Add(new ServiceReference("~/WebServices/MyWebService.asmx"));
Now we need some client-side functions, button to trigger Web Service request and a text box to provide the input for the Web Service:
• SendRequest - this function will send asyncroneus request to the Web Service
• OnComplete - this function will receive result from the Web Service
• OnError - this function will be triggered if an error occures while executing Web Service
• OnTimeOut - this function will be triggered if Web Service will not respond
• MyTextBox- text box with the input for the Web Service
• RequestButton - the button that triggers SendRequest function
The result might look like this:
Collapse

Web Service call from client-side JavaScript
<script language="javascript" type="text/javascript">
function SendRequest()
{
MySampleService.GetServerResponse(form1.MyTextBox.value, OnComplete, OnError,
OnTimeOut);
}
function OnComplete(arg)
{
alert(arg);
}
function OnTimeOut(arg)
{
alert("timeOut has occured");
}
function OnError(arg)
{
alert("error has occured: " + arg._message);
}









type="text" value="" id="MyTextBox" />
type="button" value="Send Request to the Web Service" id="RequestButton" onclick="return SendRequest()" />



This is basically it. You have a functioning client-side JavaScript code that calls server-side Web Service and treats returned response.
If we supply empty input value to the GetServerResponse method of MySampleService then as expected it will throw an exception. This exception will be caught by the OnError function in the client-side JavaScript:

Conclusion
The described client-to-server communication model where client-side JavaScript code invokes Web Service methods provides us with a lot of flexibility to extend the web site functionality. At the same time the traffic is minimal: we send only the input data required by the Web Method and we receive only the return value from the method being invoked.
History
You can always find the most up-to-date version of this article in the AJAX section of my web site.
About semenoff

define(‘APPTYPEID’, 2);//应用类型 define(‘CURSCRIPT’, ‘forum’);//当前脚本 require ‘./source/class/class_core.php’;//加载核心 require ‘./source/function/function_forum.php’; $discuz = & discuz_core::instance();//实例化 使用此种方法是为了兼容PHP4 与PHP5 复制代码 这里我们进到./source/class/class_core.php 看看这个核心类 首先来分别看看这些属性 var $db = null;//存储数据库对象 var $mem = null;//存储内存缓存对象 var $session = null;////存储session var $config = array();//存储配置数组 var $var = array();//系统常用变量 var $cachelist = array();//存储缓存数组 复制代码 我们先根据属性名来标注他们要储存的东西,这些东西肯定非常有用供以后的代码去掉用的 接着往下看看构造函数 function discuz_core() {//构造方法 $this->_init_env();//初始化环境变量 $this->_init_config();//初始化配置信息 $this->_init_input();//初始化用户输入 $this->_init_output();//初始化用户输出 } 复制代码 当然到目前为止我们都还只是猜测,我们来看看具体的代码 本帖最后由 蜗牛 于 2010-6-15 17:38 编辑 function _init_env() { //设置错误报告等级 以及关闭自动转义 error_reporting(E_ERROR); if(phpversion() < ‘5.3.0′) { set_magic_quotes_runtime(0); } //设置常用常量,最主要是判断是否支持某些函数 define(‘DISCUZ_ROOT’, substr(dirname(__FILE__), 0, -12));//程序根目录 define(‘MAGIC_QUOTES_GPC’, function_exists(‘get_magic_quotes_gpc’) && get_magic_quotes_gpc());//获取是否开启了自动转义 define(‘ICONV_ENABLE’, function_exists(‘iconv’));//iconv函数是否存在 define(‘MB_ENABLE’, function_exists(‘mb_convert_encoding’));//mb_convert_encoding函数是否存在 define(‘EXT_OBGZIP’, function_exists(‘ob_gzhandler’));//ob_gzhandler函数是否存在 define(‘TIMESTAMP’, time());//得到程序执行前的时间戳 $this->timezone_set();//设置时区,但是为何在这个方法里 没有传入8这个参数以设置我们的东八区? //加载核心函数 if(!defined(‘DISCUZ_CORE_FUNCTION’) && !@include(DISCUZ_ROOT.’./source/function/function_core.php’)) { $this->error(‘function_core.php is missing’); } //得到是否是机器人 define(‘IS_ROBOT’, checkrobot()); /* 去除’GLOBALS’ => 1, ‘_GET’ => 1, ‘_POST’ => 1, ‘_REQUEST’ => 1, ‘_COOKIE’ => 1, ‘_SERVER’ => 1, ‘_ENV’ => 1, ‘_FILES’ => 1, 外的所有超级全局变量 */ foreach ($GLOBALS as $key => $value) { if (!isset($this->superglobal[$key])) { $GLOBALS[$key] = null; unset($GLOBALS[$key]); } } global $_G; $_G = array( ‘uid’ => 0,//用户ID ‘username’ => ”,//用户名 ‘adminid’ => 0,//管理员ID ‘groupid’ => 1,//用户组 ’sid’ => ”,//加密后的SID ‘formhash’ => ”,//跟提交表单有关的HASH数据 ‘timestamp’ => TIMESTAMP,//程序执行时的时间戳 ’starttime’ => dmicrotime(),//程序开始执行的时间 ‘clientip’ => $this->_get_client_ip(),//用户IP ‘referer’ => ”,//用来记录来路URL ‘charset’ => ”,//字符编码 ‘gzipcompress’ => ”, ‘authkey’ => ”,//跟加密有关的authkey ‘timenow’ => array(), ‘PHP_SELF’ => ”,//请求的URL ’siteurl’ => ”,//站点地址 ’siteroot’ => ”,//站点根目录 ‘config’ => array(),//配置信息 ’setting’ => array(),//设置信息 ‘member’ => array(),//用户有关信息 ‘group’ => array(),//用户组信息 ‘cookie’ => array(),//cookie信息 ’style’ => array(),//风格信息 ‘cache’ => array(),//缓存数组 ’session’ => array(),//session信息 ‘lang’ => array(),//语言包 ‘my_app’ => array(), ‘my_userapp’ => array(), ‘fid’ => 0,//论坛版块ID ‘tid’ => 0,//帖子ID ‘forum’ => array(), ‘rssauth’ => ”, ‘home’ => array(), ’space’ => array(), ‘block’ => array(), ‘article’ => array(), ‘action’ => array( ‘action’ => APPTYPEID, ‘fid’ => 0, ‘tid’ => 0, ) ); //得到所执行的脚本 比如index.php $_G['PHP_SELF'] = htmlspecialchars($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']); //当前脚本 比如forum $_G['basescript'] = CURSCRIPT; //站点地址 $_G['siteurl'] = htmlspecialchars(‘http://’.$_SERVER['HTTP_HOST'].preg_replace(“/\/+(api)?\/*$/i”, ”, substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], ‘/’))).’/'); //站点目录 $_G['siteroot'] = substr($_G['PHP_SELF'], 0, -strlen(basename($_G['PHP_SELF']))); //将数据存在$var属性中 $this->var = & $_G; } 复制代码 我们从上面的代码可以看出_init_env()的作用是用来初始化程序的执行环境的,其中包含 1:得到一些环境信息,就是那段define 常量设置语句 2:得到一些常用常量: DISCUZ_ROOT 程序根目录 TIMESTAMP 程序执行时间戳 IS_ROBOT是否为机器人 3:最重要的是初始化一个全局数组(这里说并不是超级全局变量)$_G数组与 $this->var属性,其中$_G数组是供外部代码掉用的,而$this->var是类内部掉用的(这个只是猜测) 这个函数得到了一些重要信息 DISCUZ_ROOT 程序根目录 TIMESTAMP 程序执行时间戳 IS_ROBOT是否为机器人 $_G[‘timestamp’]程序执行时间 $_G[‘starttime’]程序开始执行时间 $_G[‘clientip’]访问者IP $_G['PHP_SELF']执行的脚本名称 例如index.php $_G['basescript'] 当前脚本例如forum 这个是在入口处定义的 $_G['siteurl']站点地址 $_G['siteroot'];程序根目录,这个跟DISCUZ_ROOT的区别是, DISCUZ_ROOT是绝对路径,$_G['siteroot'];是相对站点跟目录的 相对路径 本帖最后由 蜗牛 于 2010-6-15 18:01 编辑 function _init_config() { $_config = array();//初始化配置数组 @include DISCUZ_ROOT.’./config/config_global.php’;//加载配置文件 if(empty($_config)) {//配置文件不存在则提示出错 error(‘config_notfound’); } //得到加密用的密钥 $_config['security']['authkey'] = empty($_config['security']['authkey']) ? md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']) : ($_config['security']['authkey']); //将配置信息存在config属性中 $this->config = & $_config; //是否开启debug模式 if(empty($this->config['debug']) || !file_exists(libfile(‘function/debug’))) { define(‘DISCUZ_DEBUG’, false); } elseif($this->config['debug'] === 1 || $this->config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $this->config['debug']) { define(‘DISCUZ_DEBUG’, true); if($this->config['debug'] == 2) { error_reporting(E_ALL); } } else { define(‘DISCUZ_DEBUG’, false); } $GLOBALS['_G']['config'] = & $this->config; $GLOBALS['_G']['authkey'] = md5($this->config['security']['authkey'].$_SERVER['HTTP_USER_AGENT']); //静态文件目录 define(‘STATICURL’, !empty($this->config['output']['staticurl']) ? $this->config['output']['staticurl'] : ’static/’); } 这个方法做了下面几件事: 1:设置加密密钥 2:是否开启debug模式 3:将配置文件/config/config_global.php中的数据存在$this->config 属性与$_G['config']中 得到有用的数据是 1:$_G['authkey'] 全局加密密钥 2:STATICURL 静态文件目录 同时我们知道如何开启DiscuzX的debug模式 1:在配置文件/config/config_global.php中设置$_config['debug']=1或者$_config['debug']=2 如果为2就直接开启debug模式否则让DISCUZ_DEBUG常量为true,还有中方法是通过POST或者GET方式传递一个debug参数进去,并且这个参数与$_config['debug']的值一样。 当然这些所有的前提是source\function\function_debug.php文件要存在,但是官方下的程序里面并没有这个文件,也许这个是官方自己用来测试的吧 function _init_input() {//输入信息初始化 //防止注入变量 if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) { error(‘request_tainting’); } //是否开启rewrite if(!empty($_GET['rewrite'])) { $query_string = ‘?mod=’; $param = explode(‘-’, $_GET['rewrite']); $query_string .= $_GET['mod'] = $param[0]; array_shift($param); $paramc = count($param); for($i = 0;$i < $paramc;$i+=2) { $_REQUEST[$param[$i]] = $_GET[$param[$i]] = $param[$i + 1]; $query_string .= ‘&’.$param[$i].’=’.$param[$i + 1]; } $_SERVER['QUERY_STRING'] = $query_string; unset($param, $paramc, $query_string); } //如果没有开启自动转义 则将GPCF自动转义 if(!MAGIC_QUOTES_GPC) { $_GET = daddslashes($_GET); $_POST = daddslashes($_POST); $_COOKIE = daddslashes($_COOKIE); $_FILES = daddslashes($_FILES); } /*将cookie中加上了前缀的数据 去掉前缀存在$this->var['cookie']数组中*/ $prelength = strlen($this->config['cookie']['cookiepre']); foreach($_COOKIE as $key => $val) { if(substr($key, 0, $prelength) == $this->config['cookie']['cookiepre']) { $this->var['cookie'][substr($key, $prelength)] = $val; } } //是否开启diy $_GET['diy'] = empty($_GET['diy']) ? ” : $_GET['diy']; //将所有POST,GET来的数据存在$this->var['gp_'.$k]中 foreach(array_merge($_POST, $_GET) as $k => $v) { $this->var['gp_'.$k] = $v; } //mod方法,DX根据不同的MOD来掉用不同的文件,例如forum.php中require DISCUZ_ROOT.’./source/module/forum/forum_’.$mod.’.php’; $this->var['mod'] = empty($this->var['gp_mod']) ? ” : htmlspecialchars($this->var['gp_mod']); //是否是AJAX掉用的 $this->var['inajax'] = empty($this->var['gp_inajax']) ? 0 : ($_SERVER['REQUEST_METHOD'] == ‘GET’ && $_SERVER['HTTP_X_REQUESTED_WITH'] == ‘XMLHttpRequest’ || $_SERVER['REQUEST_METHOD'] == ‘POST’ ? 1 : 0); //页码 $this->var['page'] = empty($this->var['gp_page']) ? 1 : max(1, intval($this->var['gp_page'])); //用户登录后的COOKIE信息 $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? htmlspecialchars($this->var['cookie']['sid']) : ”; } 复制代码 从上面代码可以看出这个方法做了下面几件事 1:实现GPC机制,对用户提交来的数据进行自动转义 2:防止变量注入 3:启用rewrite机制后的 网址分发 4:设置COOKIE 5:将所有GET以及POST来的数据存在$this->var['gp_'.$k]中 得到的有用数据为: 1:$this->var['cookie']中存有所有COOKIE信息 2:$_GET['diy'],是否启用DIY 3:$this->var['gp_'.$k] 存有所有用户POST GET来的数据 4:$this->var['mod'],掉用哪个模块 5:$this->var['inajax']是否是AJAX掉用该页面 6:$this->var['page']页码 7:$this->var['sid'] 用户的登录验证信息 function _init_output() { //防止用户提交恶意的URL地址?疑问 为何不放在_init_input里面 if($this->config['security']['urlxssdefend'] && !empty($_SERVER['REQUEST_URI'])) { $temp = urldecode($_SERVER['REQUEST_URI']); if(strpos($temp, ‘<’) !== false || strpos($temp, ‘”‘) !== false) { error(‘request_tainting’); } } //是否开启GZIP压缩,并且不是通过AJAX掉用的并且掉用的模型不是attachment(也就是说不是附件)并且压缩函数存在 if($this->config['output']['gzip'] && empty($this->var['gp_inajax']) && $this->var['gp_mod'] != ‘attachment’ && EXT_OBGZIP) { ob_start(‘ob_gzhandler’); setglobal(‘gzipcompress’, true); } else { ob_start(); setglobal(‘gzipcompress’, false); } //是否强制输出头部信息,这里最主要是强制按照一定编码输出 if($this->config['output']['forceheader']) { @header(‘Content-Type: text/html; charset=’.$this->config['output']['charset']); } //设置$_G['charset'];为配置文件中的字符编码 setglobal(‘charset’, $this->config['output']['charset']); //设置字符编码 define(‘CHARSET’, $this->config['output']['charset']); } 复制代码 这个方法我就不多说了,他最主要是开启GZIP压缩,以及防止用户提交而已的URL,但是为什么 if($this->config['security']['urlxssdefend'] && !empty($_SERVER['REQUEST_URI'])) { $temp = urldecode($_SERVER['REQUEST_URI']); if(strpos($temp, ‘<’) !== false || strpos($temp, ‘”‘) !== false) { error(‘request_tainting’); } } 复制代码 这段代码不放在_init_input里面呢? 好了,经过实例化class_core.php 核心类之后程序所做的动作就已经读完了,这里最重要的是得到了一些有用的数据以供后面使用,最最重要的就是 $_G数组与$this->var属性 下面来看看都有哪些内容 常量: DISCUZ_ROOT 程序根目录 TIMESTAMP 程序执行时间戳 IS_ROBOT是否为机器人 STATICURL 静态文件目录 DISCUZ_DEBUG 是否开启DEBUG模式 CHARSET字符编码 $_G数组 $_G[‘timestamp’]程序执行时间 $_G[‘starttime’]程序开始执行时间 $_G[‘clientip’]访问者IP $_G['PHP_SELF']执行的脚本名称 例如index.php $_G['basescript'] 当前脚本例如forum 这个是在入口处定义的 $_G['siteurl']站点地址 $_G['siteroot'];程序根目录,这个跟DISCUZ_ROOT的区别是, DISCUZ_ROOT是绝对路径,$_G['siteroot'];是相对站点跟目录的 相对路径 $_G['authkey']全局加密密钥 $_G['charset'] 输出的编码(在配置文件中定义) $_G['cookie']中存有cookie数组 $_G['gp_'.$k]中存有所有的POST以及GET来的数据 $_G['gp_'.$k] 存有所有用户POST GET来的数据 $_G['mod'],掉用哪个模块 $_G['inajax']是否是AJAX掉用该页面 $_G['page']页码 $_G['sid'] 用户的登录验证信息 $_G['config']=$this->config=配置文件/config/config_global.php中的数据 $_GET['diy'],是否启用DIY 这里注意了$_G=&this->var 看完这个实例化之后,就是最重要的 $discuz->init();了

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧