javascript 使用GET将多个值从ajax发送到url中的php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14346695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send Multiple values from ajax to php in url with GET
提问by irshad.ahmad
I want to send 2 values to php using ajax. When I use one variable, it works fine, but when I use 2 variables, the query no longer works in the php
file.
我想使用 ajax 向 php 发送 2 个值。当我使用一个变量时,它工作正常,但是当我使用 2 个变量时,查询不再在php
文件中工作。
$.ajax({
url:'page.php?suplier_id='+suplierNameMain+'&quality_id='+qualityNameMain,
method:'GET', success:function(data) {
});
If I use only supplier_id
, everything works great.
如果我只使用supplier_id
,一切都很好。
P.S qualityNameMain
shows correct value in console.log()
PSqualityNameMain
显示正确的值console.log()
回答by Sean Bright
I'm sure it's not related, but there is no reason to build your own query string. Use the data
property instead, which as Barmar points out will properly URL encode your parameters:
我确定它不相关,但没有理由构建自己的查询字符串。改用该data
属性,正如 Barmar 指出的那样,它将正确地对您的参数进行 URL 编码:
$.ajax({
url: 'page.php',
data: {
'suplier_id': suplierNameMain,
'quality_id': qualityNameMain
},
success: function(data) {
/* Whatever */
}
});
Note that method
from your example isn't valid for jQuery (there is a type
setting to switch between GET
and POST
), but GET
is the default so you might as well exclude it altogether.
请注意,method
您的示例对 jQuery 无效(有一个type
设置可以在GET
和之间切换POST
),但GET
它是默认设置,因此您最好完全排除它。
回答by 0xmtn
Use .ajax
like this:
.ajax
像这样使用:
$.ajax({
url: 'page.php',
type: 'GET',
data: {'suplier_id': suplierNameMain,
'quality_id': qualityNameMain
}
success: function(data) {
}
);