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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:24:18  来源:igfitidea点击:

Send Multiple values from ajax to php in url with GET

phpjavascriptajax

提问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 phpfile.

我想使用 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 qualityNameMainshows 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 dataproperty 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 methodfrom your example isn't valid for jQuery (there is a typesetting to switch between GETand POST), but GETis the default so you might as well exclude it altogether.

请注意,method您的示例对 jQuery 无效(有一个type设置可以在GET和之间切换POST),但GET它是默认设置,因此您最好完全排除它。

回答by 0xmtn

Use .ajaxlike this:

.ajax像这样使用:

$.ajax({
    url: 'page.php',
    type: 'GET',
    data: {'suplier_id': suplierNameMain, 
           'quality_id': qualityNameMain
           }

    success: function(data) {
    }
 );