weixin_33690963
weixin_33690963
采纳率0%
2014-12-16 08:49

Ajax获取语法

I 've read few tutorials about ajax http request, but somehow i couldn't understand it enough to build a syntax i need.

var path = $(this).data('path');
    $.get('http://example.ro/index.php?page=contract.php&path='+path, function(){

    });

What i try to achieve is to change url (http://example.ro/index.php?page=contract.php) , without reloading the page,so i can use the path value for later use in a query or some other action,in the same page.But the url doesnt change (it must be with &path=value at the end),so i can't catch the value with something like:

if(isset($_GET['id'])){
/do something
}

Is the syntax wrong? or i cant do it in this way? Help please!

UPDATE

$(document).on('click','tr.listContractRow', function(e){

   var path = $(this).data('path');
    $.ajax({
  type: 'get',
    url: 'contract.php',
    data: {id: path},
    success: function(result) {

      console.log(result)
    },
    error: function() {

    }
});


});

The variable path is a value taken from a php content,and i want to take it with ajax and return it back so i can catch it with php.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_33729196 weixin_33729196 7年前

    Right syntax for an ajax get request made with jquery:

    $.ajax({
          type: 'get',
            url: 'yourphpscript.php',
            data: {
              key1: value1,
              keyn: valuen
            },
            success: function(result) {
              // to be executed on status 200 OK
              console.log(result) // to check what you sent back from your page
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    this is a pretty basic syntax.

    Let's assume you are on your page. And you want to call an ajax request, get something from it, store in your page, and send it back later to another php. with a submit or with another ajax.

    So step1:

    $.ajax({
          type: 'get',
            url: 'givemepath.php',
            data: {
              key1: value1,
              keyn: valuen
            },
            success: function(result) {
              // pretty ugly, but functional.
              window.path = result.path;
              // or if you plan to submit a form prepare an hidden field with id myhiddenfield
              $.('#myhiddenfield').val(result.path);
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    your givemepath.php

    <?php
      $path = ...some php code....
      header("content-Type: application/json");
      echo json_encode(array("path" => $path));
    ?>
    

    Later when you want to use again this value in your page

    $.ajax({
          type: 'get',
            url: 'sendpathbacklater.php',
            data: {
              path: window.path,
              otherdata: otherdata
            },
            success: function(result) {
              //sent it!
            },
            error: function() {
              //to be executed on status 500 error
            }
        });
    

    or just submit your form.

    点赞 评论 复制链接分享

相关推荐