如何使用ajax

时间:2018-03-16 13:14:24

标签: php jquery ajax

我试图使用ajax将视图文件中的变量传递给控制器​​文件中的 include_player_id 并执行该函数。这就是我手头的事情。

请帮我解决这个问题。我花了好几天才能成功,但我在ajax上没有成功。

View.php

<p><?php echo $ticket->last_name ?></p>
<input type="submit" class="btn-primary" value="<?php echo lang("ctn_474") ?>">

Controller.php这样

function sendMessage(){
    $content = array(
        "en" => 'Message'
        );

    $fields = array(
        'app_id' => "XXXXX-XXXXX-XXXXX-XXXXX",
        'include_player_ids' => array("XXXXXXXX-XXXXXXXX-XXXXXXXXXX"),
        'data' => array("foo" => "bar"),
        'large_icon' =>"ic_launcher_round.png",
        'contents' => $content
    );

    $fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");

我相信我需要这样的东西

<script>
  $(document).ready(function($){
    $(".btn-primary").click(function(){

        $.ajax({
          type: "POST",
          datatype:"json",
          url: BASE_URL+"/application/controllers/Ticket.php",
          data: {
            'data':<?php echo $ticket->last_name ?>
          },
          contentType:'Text',
          processData: false,              
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)

          }});

        return false;
    });  
});
</script>

1 个答案:

答案 0 :(得分:0)

感谢您发布您的帖子并提供所需信息。假设您在BASE_URL+"/application/controllers/Ticket.php"下有一个有效的HTTP端点(GET)(如果您在浏览器中调用给定的URL,则可以验证它是否正常工作)。

我们还假设您的sendMessage()函数的工作方式与预期的一样,并且不包含错误。我建议你将Controller.php改为这样的东西用于测试目的:

// static values, so we can be sure we get a predictable result for testing
$response = array['test1', 'test2'];
// rename the variable, return is a keyword and could cause problems
$result["allresponses"] = $response;
$json_result = json_encode( $result);
// comment this line, it will break the JSON.parse
// print("\n\nJSON received:\n");
// echo is just fine
echo $json_result;
// comment this line, it's not necessary
// print("\n");

现在尝试在浏览器中的给定URI下调用您的控制器。如果一切正常,你应该看到这样的东西:

  

{“allresponses”:[“test1”,“test2”]}

导致您的浏览器。

让我们来看看HTML,JS部分。

您的按钮应如下所示:

// make this a button not submit - submit is for sending forms
// give the button an ID
<input type="button" class="btn-primary" id="myCoolButton" value="Click me"></input>

您的脚本可能如下所示:

<script>
  $(document).ready(function($){
    // bind the function to the ID, otherwise all primary buttons would perform this function
    $("#myCoolButton").click(function(){

        $.ajax({
          // it's a GET endpoint as far as I understand your code, POST if creating new ressources
          type: "GET",
          datatype:"json",
          // enter your working endpoint URL here (the one you tested in your browser)
          url: BASE_URL+"/application/controllers/Ticket.php",         
          error: function(response) {console.log('ERROR '+Object.keys(response)); },
          success: function(response) {
              console.log(response)
          }});

        return false;
    });  
});
</script>

HTH