AJAX麻烦,从PHP回复

时间:2022-10-18 07:51:48

Trying to create a Mail Chimp signup form using Mail Chimp API 3.0.

尝试使用Mail Chimp API 3.0创建Mail Chimp注册表单。

So far, I have a form that validates with the jQuery Validation plugin and posts to a PHP script using jQuery Form plugin. The PHP form is working based on multiple tests. I'm also sure data is passing from the form to the PHP script, getting data back from the PHP script to the page is where things are breaking down.

到目前为止,我有一个使用jQuery Validation插件验证的表单,并使用jQuery Form插件发布到PHP脚本。 PHP表单基于多个测试工作。我也确信数据从表单传递到PHP脚本,将数据从PHP脚本返回到页面是事情发生的地方。

Here is the JS side of things.

这是JS方面的事情。

  // jquery form for submitting —— this is where the part that is not working, I just want back an error code (e.g., 400, 404, 500) or status (e.g., pending, subscribed) and pass that to the var user_status.
  submitHandler: function(form) {
  jQuery(form).ajaxSubmit({
    url: '/mc_ajax/process_mc_getstatus.php',
    success: function() {


      // translate variables form php to js //
      function reqListener () {
       console.log(this.responseText);
      }
      var oReq = new XMLHttpRequest(); //New request object
      oReq.onload = function() {
        //  var user_status = JSON.parse(this.responseText);
      };
      oReq.open("get", "/mc_ajax/process_mc_getstatus.php", true);
      oReq.send();

      alert(this.responseText);

Right now, user_status is the variable I want to populate from the PHP script.

现在,user_status是我想从PHP脚本填充的变量。

I was using var user_status = JSON.parse(this.responseText); But that caused an error since it the data is already parsed.

我使用的是var user_status = JSON.parse(this.responseText);但这导致了一个错误,因为它已经解析了数据。

Here is what I have in the PHP file for sending data back:

以下是我在PHP文件中发送数据的方法:

    // $json_data contains the output string
    $json_data = curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    // Get status from JSON //
    $json_data = json_decode(utf8_encode($json_data));
    $user_status = $json_data->status;

    echo json_encode($user_status);

This is currently resulting in an alert that says, "undefined."

目前这导致警报显示“未定义”。

1 个解决方案

#1


0  

Got it woking. Changed JS to the following:

得到它了。将JS更改为以下内容:

                // jquery form for submitting
                submitHandler: function(form) {
                    jQuery('#mc-embedded-subscribe-form').hide();
                    jQuery('#newsletter-form').append("<p class='inprogress'>Working on that for you.</p>");

                    jQuery(form).ajaxSubmit({
                        url: '/mc_ajax2/process_mc.php',

                        success: function(responseText, statusText, xhr, $user_status) {
                            jQuery('.inprogress').hide();
                            // jQuery('#newsletter-form').append(responseText);
                            jQuery('#newsletter-form').append("<p class='thanks'>" + responseText + "</p>");
                        }
                    });

#1


0  

Got it woking. Changed JS to the following:

得到它了。将JS更改为以下内容:

                // jquery form for submitting
                submitHandler: function(form) {
                    jQuery('#mc-embedded-subscribe-form').hide();
                    jQuery('#newsletter-form').append("<p class='inprogress'>Working on that for you.</p>");

                    jQuery(form).ajaxSubmit({
                        url: '/mc_ajax2/process_mc.php',

                        success: function(responseText, statusText, xhr, $user_status) {
                            jQuery('.inprogress').hide();
                            // jQuery('#newsletter-form').append(responseText);
                            jQuery('#newsletter-form').append("<p class='thanks'>" + responseText + "</p>");
                        }
                    });