0

This is my AJAX code:

$.ajax({
        type: "POST", 
        url: "ajax.php", 
        data : params+'&lakasid='+lakasid,
        dataType: "json",

This code works perfectly, but when I add one more parameter to the data field, the request will not start.

Wrong code:

$.ajax({
    type: "POST", 
    url: "ajax.php", 
    data : params+'&lakasid='+lakasid+'&action='+action,
    dataType: "json",

When I add action at the end, the code is not working.

What could be the problem?

Thanks in advance.

Tibor
  • 99
  • 2
  • 11
  • What error are you getting? Could you share the rest of that function? You are at least missing a `})`. – PhilTrep Sep 11 '16 at 11:57
  • Can't you use an anonymous object for the data? `data: { field1: "result1", field2 : "result2"}` Also I think your data url should be `params+'?lakasid='+lakasid+'&action='+action,` – Canvas Sep 11 '16 at 11:57
  • @Canvas I agree that an Object should be used for `data`, because this will ensure that escaping is done correctly. But the `?` separates the path from the params and `data` (if used for `GET`) is already the part the is appended by jQuery to the `url` using the `?`. So either `params` is a string containing already other parameters and `&` is correct or `params` is already wrong. – t.niese Sep 11 '16 at 12:00
  • What are the values of `params`, `lakasid` and `action`? – Dekel Sep 11 '16 at 12:07
  • If i console.log the code from params to action, it will look like the following: datum=2016-09-15&gaz=12&aram_nappali=13&aram_ejjeli=14&viz1=15&lakasid=19&action=save, @PhilTrep I just copied the top of the function it's much longer, but the problem I think is only with the url, because if I don't use the last parameter it is working. I don't get any errors. – Tibor Sep 11 '16 at 12:12

2 Answers2

0

Really you should use an object for data, like so:

var params = {
  'lakasid': lakasid,
  'action': action
};

$.ajax({
    type: "POST", 
    url: "ajax.php", 
    data : params,
    dataType: "json",

It would also be helpful to see the specific error you're receiving so that troubleshooting is easier.

Brandon Parker
  • 762
  • 7
  • 18
0

I believe ultimately your question can be answered here where there are multiple solutions:

How to pass parameters in $ajax POST?

Community
  • 1
  • 1
meditari
  • 131
  • 2
  • 7