Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过JSON(AJAX函数)发送数据,但无法访问相应页面的控制器_Php_Ajax_Json - Fatal编程技术网

Php 通过JSON(AJAX函数)发送数据,但无法访问相应页面的控制器

Php 通过JSON(AJAX函数)发送数据,但无法访问相应页面的控制器,php,ajax,json,Php,Ajax,Json,这是我的JavaScript函数,包含在HEAD标记中: function test() { //enter code here $.ajax({ url: 'test?msgto='+document.getElementById('Select1').value, dataType: 'json', type : 'GET', success: function(result) { // console.log(

这是我的JavaScript函数,包含在HEAD标记中:

function test()
{

//enter code here
$.ajax({
      url: 'test?msgto='+document.getElementById('Select1').value,
      dataType: 'json',
      type : 'GET',
      success: function(result)
      {
        //  console.log(result);
          alert('test1');
        //  alert(string(result[0]['Select1']) );

        //  alert(result[0]['TextArea1']);
           //document.getElementById('Text1').Value = ;// string(result[0]['location']);

      }


    });

 }
我想在一个按钮的Cilck事件上使用这个函数将数据发送到我的PHP控制器。我已经为GETACTION()编写了以下代码

但在点击时,我没有得到任何输出或结果。。。。 请尽快指导我……:)

那里有几个误用

首先,您确定GETACTION()检索/test路径吗

其次,在您的JS代码中,我不确定您调用的路径是否正确

你最好用这个:

$.ajax({
  type: 'GET',
  url: '/test', // Notice the '/' at the beginning
  data: {
    msgto: $('#Select1').val(), // Since you're using jQuery, do it all the way :)
  }
  success: function(result) {
    alert(result);
  }
});
请注意
url
参数开头的斜杠。意思是:“在域名之后开始”。另外,当您使用jQuery:)时,不需要使用verbose
document.getElementById()


最后,请参阅
数据
参数。jQuery将在发送AJAX请求之前自动构建正确的URL。我认为它可以让你的代码更干净。

HTML是什么样子的。你能把它的一部分贴出来吗。此外,行动的路径应该是相对的,如/test/?这会是个问题吗?嗨,戴维斯,真的谢谢你的关心。。。。我已经使用了以上代码提供的u。。。。!!Bu现在显示(在chrome控制台中)“测试未定义”。。。。还有,请在某种程度上指导我,如何检查它的输出。。。。。!!嗨,我不是戴维斯:)看起来你忘了“/test”周围的引号了。检查你的代码。
$.ajax({
  type: 'GET',
  url: '/test', // Notice the '/' at the beginning
  data: {
    msgto: $('#Select1').val(), // Since you're using jQuery, do it all the way :)
  }
  success: function(result) {
    alert(result);
  }
});