如何实现调用实现httpget方法的php函数的ajax并将响应作为json返回给ajax

时间:2021-11-10 13:14:51

MY AJAX function

我的AJAX功能

$(document).ready(function(){
        $("#sub").click(function()
        { 
         console.log("Ajax out");
         var isbn= $('#isbn').val();    
            $.ajax({
                type:"POST",
                url:"httprequestget.php",
                data : "isbnn="+isbn,

                success:function(response)
                {

                 console.log("Ajax - in ( response)");
                 alert(response);




                }
               });
               });

           });

MY httpgetrequest.php

我的httpgetrequest.php

$isbnn='12345';
$result2=myRestapiGET($isbnn);

 function myRestapiGET($isbn)
 {
     $url = 'http://localhost:8080/Scanbook-server/books/'.$isbn;
     $options = array(
     'http' => array
     (
    'method'  => 'GET'
     ),
                     );
       $context  = stream_context_create($options);
       $result = file_get_contents($url, false, $context);

     return $result;
 }

I want to return results as json format retrieved by get function in php to ajax.But when i try without the function to just echo some random variable I do get the response.

我想将结果作为json格式返回由php中的get函数检索到ajax.But当我尝试没有函数只是回显一些随机变量我得到了响应。

3 个解决方案

#1


0  

I think the problem here is that you are not sending the data as an object and that is why it only works whenever you try it with a random variable. Please try with the following:

我认为这里的问题是你没有将数据作为一个对象发送,这就是为什么它只在你用随机变量尝试时才有效。请尝试以下方法:

$("#sub").click(function() { 

    var isbn= $('#isbn').val();    

    $.ajax({ 
        type: 'POST', 
        url: 'httprequestget.php', 
        data: { isbn: isbn }, 
        success: function (response) {
            console.log("Ajax - in ( response)");
            alert(response); 
        }
    });

}

Hope this helps!

希望这可以帮助!

#2


0  

Not sure if I understand correctly.

不确定我是否理解正确。

Add the dataType attribute in the $.ajax function : dataType: "json"

在$ .ajax函数中添加dataType属性:dataType:“json”

And use the php json_encode to return the response in Scanbook-server/books/ script :

并使用php json_encode返回Scanbook-server / books / script中的响应:

echo json_encode(["book" => "foo", "isbn" =>13456, "response" => 'success']);

echo json_encode([“book”=>“foo”,“isbn”=> 13456,“response”=>“成功”]);

#3


0  

You need to echo the result of the function to send it back to the client:

您需要回显函数的结果以将其发送回客户端:

$isbnn = $_POST['isbnn'];
$result2=myRestapiGET($isbnn);
echo $result2;

#1


0  

I think the problem here is that you are not sending the data as an object and that is why it only works whenever you try it with a random variable. Please try with the following:

我认为这里的问题是你没有将数据作为一个对象发送,这就是为什么它只在你用随机变量尝试时才有效。请尝试以下方法:

$("#sub").click(function() { 

    var isbn= $('#isbn').val();    

    $.ajax({ 
        type: 'POST', 
        url: 'httprequestget.php', 
        data: { isbn: isbn }, 
        success: function (response) {
            console.log("Ajax - in ( response)");
            alert(response); 
        }
    });

}

Hope this helps!

希望这可以帮助!

#2


0  

Not sure if I understand correctly.

不确定我是否理解正确。

Add the dataType attribute in the $.ajax function : dataType: "json"

在$ .ajax函数中添加dataType属性:dataType:“json”

And use the php json_encode to return the response in Scanbook-server/books/ script :

并使用php json_encode返回Scanbook-server / books / script中的响应:

echo json_encode(["book" => "foo", "isbn" =>13456, "response" => 'success']);

echo json_encode([“book”=>“foo”,“isbn”=> 13456,“response”=>“成功”]);

#3


0  

You need to echo the result of the function to send it back to the client:

您需要回显函数的结果以将其发送回客户端:

$isbnn = $_POST['isbnn'];
$result2=myRestapiGET($isbnn);
echo $result2;