开发者

Ajax going to [object%20Object]

开发者 https://www.devze.com 2023-04-01 00:14 出处:网络
I\'m just trying to do a simple request like this: $(\'.asd\').change( function () { $.ajax({ url: \'http://127.0.0.1/folder/index.php/controller/action/integer\',

I'm just trying to do a simple request like this:

    $('.asd').change(
        function () {
            $.ajax({
                url: 'http://127.0.0.1/folder/index.php/controller/action/integer',
                success: function(data){}
            });
        }
    );

This code tries to go to http://127.0.0.1/folder/index.php/controller/[object%20Object] instead and 开发者_如何学运维gets a 404. Where is it pulling the object from? I'm using a simple string.


For me the issue was that I was using $.post instead of $.ajax.

// fails:
$.post({
    url: "/example/" + this.id,
    // ...
});

// works:
$.ajax({
    url: "/example/" + this.id,
    // ...
});


ajax expects a parameter map. post expects single parameters:

// fails:
$.post({
    url: "/example/" + this.id,
    // ...
});

// works:
$.post("/example/" + this.id);


I had the same issue and dug all over looking for an answer. Unfortunately, this contributor never came back with one. Mine was a stupid error. upon returning from Ajax, I used inadvertently named my variable after a reserved word. Here is what I had:

$.post('/MyApp.php', { param: 'getLocation' },
  function(xml) {
    location=$(xml).find('Location');
  }
});

Coded like this, upon return from Ajax the page is redirected to http://myurl/[Object%20object] which makes perfect sense now.

Resolution: change the "location=$..." to "clocation=$..." Hopefully this answer will help someone else. It's a tough one to debug.


For me, working in Play and using jsRoutes this was very very painful to figure out.

It seems that Play (javaScript) will do an Ajax GET without issue using the following syntax:

$.ajax(jsRoutes.controllers.MyController.ajax(inurlparam))
.done(function(data) {
  }).fail(function(data) {
  })

However (and here is the nasty part)... If you wish to do a POST you need to use the absolutURL() method on the URL first to get the ajax (or post) not to include an [object%20Object] into the url at post time. No amount of debugging the url or the form params seemed to indicate what was happening nor why. I just literally "guessed" this solution out of sheer frustration. PS: Note the brackets on the end of absolutURL() -- js people will instantly get those mean "do" the function not just pass me the handle to it.

  var url = jsRoutes.controllers.MyController.ajaxPost(inurlparam).absoluteURL();
  var formData = $('#form').serialize();
  $.ajax({
    url: url,
    type: "POST",
    data: formData
  }).done(function(data) {
  }).fail(function(data) {
  })

I'm answering this here as this is where Google lead me once I finally realized it might not be my coding that was causing the problem :)


You Must Desriallize the text to Number(Int):

var ID = parseInt( 
$(this)
  .closest("tr") //tr of DataTable 
  .find("td") //td of ....
  .eq(0)
  .text(),
10 
);
0

精彩评论

暂无评论...
验证码 换一张
取 消