search

Home  >  Q&A  >  body text

angular.js - When jQueryMobile is integrated with angularJS to make ajax requests, why does the post request fail, but the get request does?

Both the html page and the php page are sure to be correct. When using jquerymobile and angularjs to make an ajax request, why is the post request unsuccessful, but the get request is OK? The js code is as follows:

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;
  }
})
世界只因有你世界只因有你2758 days ago623

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