无法访问 $.getJSON 函数

Not able to access the $.getJSON function

本文关键字:getJSON 函数 访问      更新时间:2023-09-26

这是一个尝试访问$.getJSON的代码,但它没有响应。可能是什么原因。

        function getTerms()
        {
            alert("hello"); //it alerts hello           
            $.getJSON('http://localhost/context-search.php?callback=?', function(r) {
                //not here
                alert("Hello");
            });
        }
        $(document).ready(function(){
        alert("hello");         
        getTerms(); 
    });

上下文搜索.php以以下形式返回 JSON 数据

{"选项":["OSI","TCP","IP","HTTP Protcol","以太网","网络协议","协议含义'"]}

我哪里可能出错了??请帮助我度过难关!谢谢!!:)

您是否检查了控制台日志?如果它不包含错误,请改为执行这样的 json 请求

$.ajax({
    url: url,
    dataType: 'json',
    data: data,
    success: function(){},
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("error " + textStatus);
        console.log("incoming Text " + jqXHR.responseText);
    }
});

这将为您提供适当的错误处理。

可能是同源策略。假设它在本地主机上运行,您是否尝试过

$.getJSON('/context-search.php?callback=?', ...);

这样用

 $(document).ready(function(){
  $.getJSON('/ITTA/SavePreference/?t=' + new Date(), { id: id3 },
                   function (data) {
                       if (data != null && data != undefined && data != '') {
                           top.location.href = '/ITTA/Entries/' + id3;
                       }
                   }); });