You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JQuery报错:Uncaught TypeError: Cannot read property 'result' of undefined 求助

解决"Uncaught TypeError: Cannot read property 'result' of undefined"问题

这个报错核心是前端AJAX接收的data里找不到domain_whois属性,大概率是前后端请求不匹配、后端返回格式不符合预期导致的,咱们一步步排查修复:

1. 修复前后端请求方式不匹配问题

你的前端AJAX用的是type: 'get',但后端test.php里却用$_POST获取参数,这会直接导致$a和$b为空,请求目标接口时参数无效,后续返回的内容自然不符合预期。

修改test.php的参数获取方式为$_GET:

error_reporting(E_ERROR ); 
$a = $_GET["domain"]; 
$b = $_GET["domain_ext"]; 

function httpGet($url) { 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    $output=curl_exec($ch); 
    curl_close($ch); 
    return $output; 
}

// 先获取目标接口的返回内容
$raw_response = httpGet("https://xxxx.jsp?domain=$a$b");

2. 确保后端返回符合前端预期的JSON格式

前端dataType: 'json'要求后端返回合法JSON,且结构必须包含domain_whois.result。如果目标https://xxxx.jsp返回的是纯文本,需要手动包装成前端需要的结构:

// 把纯文本结果包装成预期的JSON结构
echo json_encode([
    'domain_whois' => [
        'result' => $raw_response
    ]
]);

你可以先直接访问test.php?domain=test&domain_ext=.com,查看返回的内容是否是标准JSON,且结构符合{"domain_whois": {"result": "xxx"}}。

3. 修正前端表单选择器不匹配问题

你的HTML表单ID是form,但JS里用的是$('#domain_' + mode + '_form')(当mode为whois时,选择器是#domain_whois_form),这会导致无法获取输入框的值,AJAX发送空参数。

修改HTML表单的ID为domain_whois_form:

<form class="smallForm" id="domain_whois_form" onsubmit="event.returnValue = false; return false">
  Name: <input type = "text" name = "domain" /> 
  <select name="domain_ext" class="inputAuto">
    <option value=".com">.com</option>
  </select> 
  <input type = "submit" /> 
</form>

4. 增加AJAX错误排查回调

在AJAX里添加error回调,方便快速定位请求失败的原因:

$.ajax({
    url: '//xxxx.com/test.php',
    type: 'get',
    dataType: 'json',
    data: 'domain=' + enc_domain + '&domain_ext=' + enc_domain_ext + '&form=domain_' + mode + '_form',
    error: function(jqxhr, status, error) {
        console.log('请求出错:', status, error);
        domain_loaded(mode);
    }
}).always(domain_ajax_always_ffactory(mode, enc_domain, enc_domain_ext));

做完这些调整后,应该就能正常获取并显示查询结果了。

内容的提问来源于stack exchange,提问作者user9053749

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:34:39