PHP function call with jquery $ .ajax return json

How can I (at least possibly) call a PHP function from javascript to target a single method similar to ASP.NET.

PHP:

...
function a($some_string){
return json_encode(array(
                "username" => "bob",
                "items" => array(
                        "item1" => "sandwich",
                        "item2" => "applejuice"
                )
        ));
}

JS:

    $.ajax(
        {url:"index.php/a", 
        type:"POST",
        contentType:"application/json; charset=utf-8",
                    data:{some_string:"blabla"},
        dataType:"json",
        success:function(data){
            alert(data);
            },
        error:function(a,b,c){
            }
        });

In C #:

[WebMethod()]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public static object a(string some_string)
{
  return new { 
                user_name = "bob", 
                items = new { 
                    item1 = "sanwitch", 
                    item2 = "applejuice" 
                    }
                };
        }

Thanks
Peter

+3
source share
2 answers

You can just put this one function only in a PHP file. Then run the function inside this file (so that it selects JSON). Ask ajax to call this file.

In your case:

echo a($string);
+2
source

JQuery, XAJAX, PHP AJAX. , . , PHP ( JAVASCRIPT), - .

0

Source: https://habr.com/ru/post/1779264/


All Articles