如何在Angular 2中处理纯文本,服务器响应?

时间:2016-02-15 22:25:07

标签: typescript angular

如何在Angular 2中处理纯文本服务器响应?

我使用以下内容:

    this.http.get('lib/respApiTest.res')
    .subscribe(testReadme => this.testReadme = testReadme);

LIB / respApiTest.res

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim 
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea

但我得到一个JSON格式示例:

{"_body":"Lorem ipsum dolor sit amet......"}

现在我正在使用以下内容来处理:(this.testReadme._boby)

    alert(this.testReadme._boby);

这很有效,但这是处理Angular 2中纯文本的方法吗?

1 个答案:

答案 0 :(得分:12)

使用response.text()方法......

//if($_POST["w32apideftype"]!="")
//{     
    //echo $_POST["key"];
     // Function for filtering input values.
    function test_input($data)
    {
        $data = trim($data);
        $data =stripslashes($data);
        $data =htmlspecialchars($data);
        return $data;
    }
    //echo "ths i stest";
    // Initialize variables to null.
    $s_name="ABC";// Sender Name
    $s_email="feedback@abc.com";//Sender's Email
    $r_email="xyz <xyz.com>";//Recipient Email
    $subject1="My subject";//Subject of mail
    //Form Data vars
    $f_name =""; 
    $f_email =""; // Sender's email ID
    $f_message =""; // Sender's Message
    $f_phone="";//Sender's Phone
    $f_city="";//Sender's city
    //Form data vars end

    $nameError ="";
    $emailError ="";
    $f_phoneError="";
    $f_cityError="";
    $subjectError ="";
    $f_messageError ="";
    $successMessage =""; // On submittingform below function will execute.
    if(isset($_POST['MasterInner_submit'])) { // Checking null values in message.
        //echo "Submit button pressed";

        if (empty($_POST["MasterInner_txtName"])){
            $nameError = "Name is required";
            //echo $nameError;
        }
        else
         {          
            if (!preg_match("/^[a-zA-Z ]*$/",$f_name))
            {
                $nameError = "Only letters and white space allowed";
            }
            else
            {
                $f_name = test_input($_POST["MasterInner_txtName"]); // check name only contains letters and whitespace
                //echo $name;
            }
        } // Checking null values inthe message.
        if (empty($_POST["MasterInner_txtEmail"]))
        {
            $emailError = "Email is required";
            //echo $emailError;
        }
        else
        {
            $f_email = test_input($_POST["MasterInner_txtEmail"]);
            //echo $email;
        } // Checking null values inmessage.
        if (empty($_POST["MasterInner_txtRemarks"]))
        {
            $f_messageError = "Message is required";
            $f_message="No Additional message";
            //echo $f_messageError;
        }
        else
        {
            $f_message = test_input($_POST["MasterInner_txtRemarks"]);
            //echo $f_message;
        } // Checking null values inthe message.
        if (empty($_POST["MasterInner_txtMobile"]))
        {
            $f_phoneError = "Genuine phone is required";
            //echo $f_phoneError;
        }
        else
        {

            $f_phone = test_input($_POST["MasterInner_txtMobile"]);
            //echo $f_phone;
        } // Checking null values inmessage.

        if ($_POST["MasterInner_txtCity"]=="Not Selected")
        {
            $f_cityError = "city is required";
            //echo $f_cityError;
        }
        else
        {
            $f_city = test_input($_POST["MasterInner_txtCity"]);
            //echo $f_city;
        } // Checking null values inmessage.

        if( !($f_name=='') && !($f_email=='') &&!($f_phone=='') &&!($f_city==''))
        { // Checking valid email.
            if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$f_email))
            {
                $email=str_replace(",","",$f_email);

                $header1  = 'MIME-Version: 1.0' . "\r\n";
                $header1 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $header1 .= 'To: '.$r_email."\r\n";
                $header1 .= 'From: '.$s_name.'<'. $s_email .'>' . "\r\n";
                $header1 .= 'Cc:' . "\r\n";
                $header1 .= 'Bcc:' . "\r\n";


                /* Let's prepare the message for the e-mail */
                //$msg = wordwrap($msg, 70, "\r\n");
                $msg1 = '<html><head><title>Enquiry</title></head>
                    <body><table style="font-size:16px;font-weight:bold;border:3px solid #e0781f;"bgcolor="#dededc" align="center" width="500"><tr><td colspan="2"><h1>'.$f_name.' sent a Enquiry</h1></td></tr>
                    <tr><td>Name: </td><td>'.$f_name.'</td></tr>
                    <tr><td>E-mail: </td><td>'.$f_email.'</td></tr>
                    <tr><td>Phone: </td><td>'.$f_phone.'</td></tr>
                    <tr><td>city: </td><td>'.$f_city.'</td></tr>
                    <tr><td>Message: </td><td>'.$f_message.'</td></tr></table>
                    </body></html>'; 
                /* Send the message using mail() function */
                //echo "message1: - $msg and message 2 : - $msg1";


                if(mail($s_email,$subject1,$msg1,$header1))
                {
                    $successMessage = "Message sent successfully.......";
                    echo"Thank You";
                    header("location:".$_POST["w32apideftype"]);
                }   
            }
            else
            {
                $emailError = "Invalid Email";
                //echo"Invalid Email";

            }
         }
    }
//}