Home  >  Q&A  >  body text

angular.js - jQueryMobile与angularJS整合进行ajax请求时,为什么post请求不成功,而get请求就可以呢?

html页面和php页面都确定没错,使用jquerymobile和angularjs结合进行ajax请求时,为什么post请求不成功,而get请求就可以呢?js代码如下:

angular.module('kaifanla',['ng','ngTouch']).controller('orderCtrl',function ($scope,$http,parseSearch) {
  $scope.isSubmit=false;
  var did=parseSearch(location.search).did;
  $scope.order={did:did};
  $scope.submit=function () {
    $scope.isSubmit=true;
   // var str=jQuery.param($scope.order);
   //  $http.get('../data/order_add.php?did='+did+'&user_name='+$scope.order.user_name+'&sex='+$scope.order.sex+'&phone='+$scope.order.phone+'&addr='+$scope.order.addr).
   //  success(function (data) {
   //    console.log(data);
   //    $scope.oid=data.oid;
   //  })
    var str=jQuery.param($scope.order);
    $http.post('../data/order_add.php',str).success(function (data) {
      console.log(data);
    })
  }
  }).
service('parseSearch', function () {
  return function (search) {   //可以在所有的Controller中使用的一个函数——Service
    /*将形如'?did=2&pno=3&uname=tom&loc=bj转换为一个对象'*/
    var result = {};
    search = search.substring(1);
    var arr = search.split('&');  //['did=3', 'pno=5', 'uname=tom']
    angular.forEach(arr, function (v, k) {
      var kv = v.split('=');
      result[kv[0]] = kv[1];
    })
    return result;
  }
})
世界只因有你世界只因有你2737 days ago595

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-15 17:12:20

    Does your php backend have a post interface? I think you only opened the get type interface.

    In addition, I don’t know what the agreed data format of your backend is. The data you have here is a string passed through jq’s param. If you do not specify the header of the content format to be sent, your backend may not know it. How to parse. Or directly parse the formbody as a string.

    A previous article may be useful to you
    http://blog.anchengjian.com/#...

    reply
    0
  • Cancelreply