使用GET将多个值从ajax发送到url中的php [英] Send Multiple values from ajax to php in url with GET

查看:55
本文介绍了使用GET将多个值从ajax发送到url中的php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ajax将2个值发送到php.当我使用一个变量时,它工作正常,但是当我使用2个变量时,查询在 php 文件中不再起作用.

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({ 
    url:'page.php?suplier_id='+suplierNameMain+'&quality_id='+qualityNameMain,
        method:'GET', success:function(data) {
});

如果我仅使用 supplier_id ,那么一切都会很好.

If I use only supplier_id, everything works great.

PS qualityNameMain console.log()

推荐答案

我确定它不相关,但是没有理由构建自己的查询字符串.改用 data 属性,正如Barmar指出的那样,该属性将正确地对您的参数进行URL编码:

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:

$.ajax({
    url: 'page.php',
    data: {
        'suplier_id': suplierNameMain,
        'quality_id': qualityNameMain
    },
    success: function(data) {
        /* Whatever */
    }
});

请注意,示例中的方法不适用于jQuery(存在 type 设置,可在 GET 之间切换POST ),但默认为 GET ,因此您也可以将其完全排除.

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.

这篇关于使用GET将多个值从ajax发送到url中的php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆